为什么春豆不能注入石英作业?

时间:2016-07-01 01:57:03

标签: spring null autowired

我现在正在写一个石英作业,每天凌晨3点开始,以保​​存数据库和redis中的数据。但是现在我遇到了一个问题,即使我尝试了很多次也无法解决问题。

工作:

public class QuartzScheduler implements ServletContextListener {

private static Logger logger = Logger.getLogger(QuartzScheduler.class);

@Autowired
private ExchangeRateConfigBean exchangeRateConfigBean;

private  Scheduler scheduler = null;

@Override
public void contextInitialized(ServletContextEvent servletContext) {

    try {
        ExchangeRateConfig exchangeRateConfig = exchangeRateConfigBean.setUpConfigurationFromConfigFile();

        if(!exchangeRateConfig.getJob_fire()) {
            logger.info("ExchangeRate Job Info: Current Exchange Rate Job flag is not open. Wont start the job!");
            return;
        }
    } catch (Exception e) {
        logger.error("ExchangeRate Job Info: Something wrong when analyzing config.properties file!");
        logger.error(e.getMessage());
        logger.error(e.getStackTrace());
        logger.error(e.getCause());
        return;
    }


    try {

        scheduler = StdSchedulerFactory.getDefaultScheduler();

        scheduler.start();

        logger.info("ExchangeRate Job Info: Exchange Rate job starting......");
    } catch (SchedulerException ex) {
        logger.error(ex);
    }
}

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
    try {
        scheduler.shutdown();
    } catch (SchedulerException e) {
        logger.error(e);
    }
}

在这个作业中,我自动装配了组件ExchangeRateConfigBean,但是当我调试时,我看到该实例为null。

下面的弹簧配置:

  <bean id="exchangeRateConfigBean" class="com.letv.exchangerate.bean.impl.ExchangeRateConfigBeanImpl">
    <constructor-arg ref="exchangeRateErrorBean" />
</bean>
<bean id="exchangeRateErrorBean" class="com.letv.exchangerate.bean.impl.ExchangeRateErrorBeanImpl">
</bean>

下面的配置Bean:

public class ExchangeRateConfigBeanImpl implements ExchangeRateConfigBean {

public ExchangeRateConfigBeanImpl(){}

public ExchangeRateConfigBeanImpl(ExchangeRateErrorBean exchangeRateErrorBean)
{
    this.exchangeRateErrorBean = exchangeRateErrorBean;
}

private static Logger logger = Logger.getLogger(ExchangeRateConfigBeanImpl.class.getClass());

private ExchangeRateErrorBean exchangeRateErrorBean;

@Override
public ExchangeRateConfig setUpConfigurationFromConfigFile() throws IOException {

    InputStream inputStream = null;
    ExchangeRateConfig exchangeRateConfig = null;

    try {

        Properties prop = new Properties();
        String propFileName = "config.properties";

        inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
        exchangeRateConfig = new ExchangeRateConfig();

        if (inputStream != null) {
            prop.load(inputStream);
        } else {
            logger.error("property file '" + propFileName + "' not found in the classpath");
            throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
        }

        String job_fire_tmp = prop.getProperty("job_fire");
        exchangeRateConfig.setJob_fire(job_fire_tmp.isEmpty() ? false : Boolean.parseBoolean(job_fire_tmp));

        return exchangeRateConfig;

    } catch (IOException e) {
        logger.error(e.getStackTrace());
        return exchangeRateConfig;
    } finally {
        inputStream.close();
    }
}

在config bean中,我使用构造函数注入来注入上面的错误bean。错误bean的代码列表如下:

public class ExchangeRateErrorBeanImpl implements ExchangeRateErrorBean{

public ExchangeRateErrorBeanImpl(){}

@Override
public ExchangeRateError getExchangeRateError(String content) {

    if (content.isEmpty())
        return null;
    if (!content.contains(":"))
        return null;

    String[] strArray = content.split(":");

    return new ExchangeRateError(strArray[0], strArray[1]);
}

}

任何人都可以提供帮助?为什么我在作业类中自动装配Config bean,它会创建null实例? THX。

在发布此帖之前,我已经详细阅读了此内容,Why is my Spring @Autowired field null? 但它没有节省我的时间。

1 个答案:

答案 0 :(得分:0)

您的代码将QuartzScheduler显示为ServletContextListener的一个实例。这只会创建一个Java bean。您的Quartz Scheduler类必须是Spring applicationContext中的bean。只有这样,其他依赖项才会自动连接。请参阅此示例 - http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-quartz

相关问题