springboot整合quartz,报空指针错误,无法取得bean

Spring有自己的Schedule定时任务,在Spring boot中使用的时候,不能动态管理JOB,于是就使用Quartz来实现。

在Spring Boot中配置Quartz:

[java]  view plain  copy
  1. import java.io.IOException;  
  2. import java.util.Properties;  
  3.   
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.beans.factory.config.PropertiesFactoryBean;  
  6. import org.springframework.context.annotation.Bean;  
  7. import org.springframework.context.annotation.Configuration;  
  8. import org.springframework.core.io.ClassPathResource;  
  9. import org.springframework.scheduling.annotation.EnableScheduling;  
  10. import org.springframework.scheduling.quartz.SchedulerFactoryBean;  
  11.   
  12. @Configuration  
  13. @EnableScheduling  
  14. public class QuartzSchedule {  
  15.   
  16.     @Autowired  
  17.     private MyJobFactory myJobFactory;  
  18.   
  19.     @Bean  
  20.     public SchedulerFactoryBean schedulerFactoryBean() throws IOException {  
  21.         SchedulerFactoryBean factory = new SchedulerFactoryBean();  
  22.   
  23.         factory.setOverwriteExistingJobs(true);  
  24.   
  25.         // 延时启动  
  26.         factory.setStartupDelay(20);  
  27.   
  28.         // 加载quartz数据源配置  
  29.         factory.setQuartzProperties(quartzProperties());  
  30.   
  31.         // 自定义Job Factory,用于Spring注入  
  32.         factory.setJobFactory(myJobFactory);  
  33.   
  34.         return factory;  
  35.     }  
  36.   
  37.     /** 
  38.      * 加载quartz数据源配置 
  39.      *  
  40.      * @return 
  41.      * @throws IOException 
  42.      */  
  43.     @Bean  
  44.     public Properties quartzProperties() throws IOException {  
  45.         PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();  
  46.         propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));  
  47.         propertiesFactoryBean.afterPropertiesSet();  
  48.         return propertiesFactoryBean.getObject();  
  49.     }  
  50.   
  51. }  

为了在JOB中使用Spring管理的Bean,需要重新定义一个Job Factory:

[java]  view plain  copy
  1. @Component  
  2. public class MyJobFactory extends AdaptableJobFactory {  
  3.       
  4.     @Autowired  
  5.     private AutowireCapableBeanFactory capableBeanFactory;  
  6.   
  7.     @Override  
  8.     protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {  
  9.         // 调用父类的方法  
  10.         Object jobInstance = super.createJobInstance(bundle);  
  11.         // 进行注入  
  12.         capableBeanFactory.autowireBean(jobInstance);  
  13.         return jobInstance;  
  14.     }  
  15. }<span style="font-size:14px;">  
  16. </span>  
然后在JOB中就可以使用Spring管理的Bean了

 

[java]  view plain  copy
  1. public class MyJob implements Job, Serializable {  
  2.     private static final long serialVersionUID = 1L;  
  3.     private Logger logger = LoggerFactory.getLogger(this.getClass());  
  4.   
  5.     @Autowired  
  6.     private SomeService someService;  
  7.   
  8.     @Override  
  9.     public void execute(JobExecutionContext context) throws JobExecutionException {  
  10.         someService.doSomething();  
  11.     }  
  12. }  

猜你喜欢

转载自blog.csdn.net/yzc11111111/article/details/80635475