2016-07-01 9 views
0

私は毎日午前3時に開始する石英ジョブを作成しています。しかし、今は問題があり、何度も試してみても解決できません。石英の仕事で春の豆を注入できないのはなぜですか?

仕事:このジョブで

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をautowiredが、私はでデバッグするとき、私は、インスタンスが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> 

設定ビーン:コンフィグ豆で

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(); 
    } 
} 

は、私は上記のエラー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]); 
} 

}

誰でも助けることができる:以下のエラーBeanのコードリスト?なぜ私はジョブクラスでConfig Beanをオートワイヤードしたのですか、それはヌルインスタンスを作成しますか?どうも。

私がこれを投稿する前に、私はこれを詳細に読んだ、Why is my Spring @Autowired field null? しかし、それは私の時間を節約できませんでした。

答えて

0

あなたのコードは、ServletContextListenerのインスタンスとしてQuartzSchedulerを示しています。これにより、Java Beanが作成されます。あなたのQuartz Schedulerクラスは、SpringのapplicationContext内のBeanでなければなりません。そうすると他の依存関係は自動的に実行されます。例えばこれを参照してください - http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-quartz

+0

はい、正しくあります。 – CharlieShi

関連する問題