title: Why spring bean can't be injected in quartz job?
body: <p>I am now writing a quartz job to start at 3:00 am every day to save data in database and redis. But now I got a problem and can't get it solved even though I tried a lot of times.</p>
  
  <p>The job:</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>In this job , I autowired the component ExchangeRateConfigBean, but when I debugged in, I saw that the instance is null. </p>
  
  <p>the spring config below:</p>
  
  <pre><code>  &lt;bean id="exchangeRateConfigBean" class="com.letv.exchangerate.bean.impl.ExchangeRateConfigBeanImpl"&gt;
      &lt;constructor-arg ref="exchangeRateErrorBean" /&gt;
  &lt;/bean&gt;
  &lt;bean id="exchangeRateErrorBean" class="com.letv.exchangerate.bean.impl.ExchangeRateErrorBeanImpl"&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>The config Bean below:</p>
  
  <pre><code>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();
      }
  }
  </code></pre>
  
  <p>In the config bean, I used the constructor injection to inject the error bean above. the error bean's code lists below:</p>
  
  <pre><code>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]);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Anyone can help? why I autowired the Config bean in the job class, it will create null instance? thx.</p>
  
  <p>And before I post this, I have read this in detail, <a href="https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null">Why is my Spring @Autowired field null?</a>
  but it didn't save my time. </p>