quartz job 注入 spring 管理的 bean

转载自 https://blog.csdn.net/leocnx/article/details/54573275

背景:

在quartz job 里调用spring 容器管理的bean,出现空指针异常,即没有注入成功,在网上搜了很多类似的问题,最终发现都是copy改,经过各种折腾算是解决了问题。

解决方法有两种:

第一种方式:不去继承QuartzJobBean,只是一个独立的job bean,通过配置MethodInvokingJobDetailFactoryBean来实现;

第二种方式:继承QuartzJobBean,但是要重写SchedulerFactoryBean的jobFactory。


首先说明下版本:

[html] view plain copy
  1. <spring.version>4.0.2.RELEASEspring.version>  
  2. <quartz.version>2.2.2quartz.version>  

代码结构如下:

quartz job 注入 spring 管理的 bean_第1张图片

说明:

MyJobAlone为不用继承方式实现的job bean

MyJobExtend为使用继承方式实现的job bean


MyQuartzJobFactory为重写的jobFactory,用于实现在使用继承方式时注入spring管理的bean


SayServiceImpl为spring 管理的bean

-SayService.java

[java] view plain copy
  1. package top.auok.quartz.bean;  
  2.   
  3. public interface SayService {  
  4.   
  5.     void sayHello();  
  6. }  

-SayServiceImpl.java
[java] view plain copy
  1. package top.auok.quartz.bean;  
  2.   
  3. import org.springframework.stereotype.Service;  
  4.   
  5. @Service("sayService")  
  6. public class SayServiceImpl implements SayService {  
  7.   
  8.     @Override  
  9.     public void sayHello() {  
  10.         System.out.println("hello, i'm invoked by quartz.");  
  11.     }  
  12.   
  13. }  

-applicationContext.xml
[html] view plain copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xmlns:aop="http://www.springframework.org/schema/aop"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans      
  9.                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd      
  10.                         http://www.springframework.org/schema/context      
  11.                         http://www.springframework.org/schema/context/spring-context-3.1.xsd      
  12.                         http://www.springframework.org/schema/mvc      
  13.                         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd  
  14.                         http://www.springframework.org/schema/tx     
  15.                         http://www.springframework.org/schema/tx/spring-tx.xsd  
  16.                         http://www.springframework.org/schema/aop      
  17.                         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">  
  18.       
  19.     <context:component-scan base-package="top.auok.quartz" />  
  20.       
  21. beans>   


applicationContext-quartz-alone.xml为非继承方式的配置文件

applicationContext-quartz-extend.xml为继承方式的配置文件


第一种方式:(非继承)

1.job bean - MyJobAlone.java代码如下:

[java] view plain copy
  1. package top.auok.quartz;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.springframework.stereotype.Component;  
  6.   
  7. import top.auok.quartz.bean.SayService;  
  8.   
  9. @Component("myJobAlone")  
  10. public class MyJobAlone {  
  11.   
  12.     @Resource  
  13.     private SayService sayService;  
  14.   
  15.     public void execute() {  
  16.         sayService.sayHello();  
  17.     }  
  18. }  

2.配置 - applicationContext-quartz-alone.xml:
[html] view plain copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xmlns:aop="http://www.springframework.org/schema/aop"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans      
  9.                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd      
  10.                         http://www.springframework.org/schema/context      
  11.                         http://www.springframework.org/schema/context/spring-context-3.1.xsd      
  12.                         http://www.springframework.org/schema/mvc      
  13.                         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd  
  14.                         http://www.springframework.org/schema/tx     
  15.                         http://www.springframework.org/schema/tx/spring-tx.xsd  
  16.                         http://www.springframework.org/schema/aop      
  17.                         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">  
  18.       
  19.       
  20.     <bean id="aloneJobDetail"  
  21.         class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  22.         <property name="targetObject">  
  23.             <ref bean="myJobAlone" />  
  24.         property>  
  25.         <property name="targetMethod">  
  26.             <value>executevalue>  
  27.         property>  
  28.     bean>  
  29.   
  30.       
  31.     <bean id="aloneTrigger"  
  32.         class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
  33.         <property name="jobDetail" ref="aloneJobDetail">property>  
  34.         <property name="cronExpression" value="0/2 * * * * ?">property>  
  35.     bean>  
  36.   
  37.       
  38.     <bean id="SpringJobSchedulerFactoryBean"  
  39.         class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  40.         <property name="triggers">  
  41.             <list>  
  42.                 <ref bean="aloneTrigger" />  
  43.             list>  
  44.         property>  
  45.     bean>     
  46. beans>   


第二种方式:(继承)

1.job bean - MyJobExtend.java代码如下:

[java] view plain copy
  1. package top.auok.quartz;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.quartz.JobExecutionContext;  
  6. import org.quartz.JobExecutionException;  
  7. import org.springframework.scheduling.quartz.QuartzJobBean;  
  8.   
  9. import top.auok.quartz.bean.SayService;  
  10.   
  11. public class MyJobExtend extends QuartzJobBean {  
  12.   
  13.     @Resource  
  14.     private SayService sayService;  
  15.   
  16.     @Override  
  17.     protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {  
  18.   
  19.         sayService.sayHello();  
  20.     }  
  21.   
  22. }  

2.jobFactory - 重写 -MyQuartzJobFactory.java
[java] view plain copy
  1. package top.auok.quartz;  
  2.   
  3. import org.quartz.spi.TriggerFiredBundle;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.beans.factory.config.AutowireCapableBeanFactory;  
  6. import org.springframework.scheduling.quartz.SpringBeanJobFactory;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. @Component("myQuartzJobFactory")  
  10. public class MyQuartzJobFactory extends SpringBeanJobFactory {  
  11.   
  12.     @Autowired  
  13.     private AutowireCapableBeanFactory beanFactory;  
  14.   
  15.     /** 
  16.      *  
  17.      * 这里覆盖了super的createJobInstance方法,对其创建出来的类再进行autowire。 
  18.      *  
  19.      */  
  20.     @Override  
  21.     protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {  
  22.   
  23.         Object jobInstance = super.createJobInstance(bundle);  
  24.   
  25.         beanFactory.autowireBean(jobInstance);  
  26.   
  27.         return jobInstance;  
  28.   
  29.     }  
  30. }  

3.配置 - applicationContext-quartz-extend.xml:
[html] view plain copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xmlns:aop="http://www.springframework.org/schema/aop"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans      
  9.                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd      
  10.                         http://www.springframework.org/schema/context      
  11.                         http://www.springframework.org/schema/context/spring-context-3.1.xsd      
  12.                         http://www.springframework.org/schema/mvc      
  13.                         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd  
  14.                         http://www.springframework.org/schema/tx     
  15.                         http://www.springframework.org/schema/tx/spring-tx.xsd  
  16.                         http://www.springframework.org/schema/aop      
  17.                         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">  
  18.   
  19.       
  20.     <bean id="extendJobDetail"  
  21.         class="org.springframework.scheduling.quartz.JobDetailFactoryBean">  
  22.         <property name="jobClass" value="top.auok.quartz.MyJobExtend" />  
  23.               
  24.         <property name="durability" value="true" />   
  25.     bean>  
  26.   
  27.       
  28.     <bean id="extendTrigger"  
  29.         class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
  30.         <property name="jobDetail" ref="extendJobDetail">property>  
  31.         <property name="cronExpression" value="0/2 * * * * ?">property>  
  32.     bean>  
  33.   
  34.       
  35.     <bean id="SpringJobSchedulerFactoryBean"  
  36.         class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  37.         <property name="triggers">  
  38.             <list>  
  39.                 <ref bean="extendTrigger" />  
  40.             list>  
  41.         property>  
  42.         <property name="jobFactory" ref="myQuartzJobFactory">property>  
  43.     bean>     
  44. beans>   


调试运行:

[java] view plain copy
  1. package top.auok.quartz;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. public class App {  
  6.     public static void main(String[] args) {  
  7.   
  8.         // init spring ioc container  
  9.         new ClassPathXmlApplicationContext("applicationContext.xml""applicationContext-quartz-extend.xml");  //按需更改配置文件alone/extend  
  10.     }  
  11. }  

the end


附:pom.xml

[html] view plain copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0modelVersion>  
  4.   
  5.     <groupId>top.auokgroupId>  
  6.     <artifactId>quartzartifactId>  
  7.     <version>0.0.1-SNAPSHOTversion>  
  8.     <packaging>jarpackaging>  
  9.   
  10.     <name>quartzname>  
  11.     <url>http://maven.apache.orgurl>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  15.         <spring.version>4.0.2.RELEASEspring.version>  
  16.         <slf4j.version>1.7.7slf4j.version>  
  17.         <log4j.version>1.2.17log4j.version>  
  18.         <quartz.version>2.2.2quartz.version>  
  19.     properties>  
  20.       
  21.     <dependencies>  
  22.         <dependency>  
  23.             <groupId>org.springframeworkgroupId>  
  24.             <artifactId>spring-context-supportartifactId>  
  25.             <version>${spring.version}version>  
  26.         dependency>  
  27.           
  28.         <dependency>  
  29.             <groupId>org.springframeworkgroupId>  
  30.             <artifactId>spring-txartifactId>  
  31.             <version>${spring.version}version>  
  32.         dependency>  
  33.           
  34.         <dependency>    
  35.             <groupId>org.quartz-schedulergroupId>    
  36.             <artifactId>quartzartifactId>    
  37.             <version>${quartz.version}version>    
  38.         dependency>  
  39.           
  40.         <dependency>  
  41.             <groupId>log4jgroupId>  
  42.             <artifactId>log4jartifactId>  
  43.             <version>${log4j.version}version>  
  44.         dependency>  
  45.         <dependency>  
  46.             <groupId>org.slf4jgroupId>  
  47.             <artifactId>slf4j-apiartifactId>  
  48.             <version>${slf4j.version}version>  
  49.         dependency>  
  50.         <dependency>  
  51.             <groupId>org.slf4jgroupId>  
  52.             <artifactId>slf4j-log4j12artifactId>  
  53.             <version>${slf4j.version}version>  
  54.         dependency>  
  55.     dependencies>  
  56.   
  57.     <build>  
  58.         <plugins>  
  59.             <plugin>  
  60.                 <artifactId>maven-compiler-pluginartifactId>  
  61.                 <version>2.0.2version>  
  62.                 <configuration>  
  63.                     <source>1.8source>  
  64.                     <target>1.8target>  
  65.                     <encoding>utf8encoding>  
  66.                 configuration>  
  67.             plugin>  
  68.         plugins>  
  69.     build>  
  70. project>  


代码下载

你可能感兴趣的:(Quartz)