Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 13577|回复: 0

quartz整合spring框架service层对象注入为null解决方案

[复制链接]
  • TA的每日心情
    奋斗
    2024-11-24 15:47
  • 签到天数: 804 天

    [LV.10]以坛为家III

    2053

    主题

    2111

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    726782
    发表于 2021-4-20 16:08:21 | 显示全部楼层 |阅读模式

    Job实现类代码

     1 package cn.itcast.quartz;
     2 
     3 import org.quartz.Job;
     4 import org.quartz.JobExecutionContext;
     5 import org.quartz.JobExecutionException;
     6 import org.springframework.beans.factory.annotation.Autowired;
     7 import org.springframework.stereotype.Service;
     8 
     9 import cn.itcast.service.HelloServiceImpl;
    10 
    11 /**      
    12  * @author: 攻城狮小白
    13  * @creationTime:2017年11月20日 下午5:21:28
    14  */
    15 @Service
    16 public class HelloJob implements Job{
    17 
    18     @Autowired
    19     private HelloServiceImpl helloServiceImpl;
    20     public void execute(JobExecutionContext context) throws JobExecutionException {
    21         helloServiceImpl.sayHello();
    22     }
    23 
    24 }
    HelloJob.java

    业务实现类代码

     1 package cn.itcast.service;
     2 
     3 import org.springframework.stereotype.Service;
     4 
     5 /**      
     6  * @author: 攻城狮小白
     7  * @creationTime:2017年11月20日 下午5:42:12
     8  */
     9 @Service
    10 public class HelloServiceImpl {
    11     public void sayHello(){
    12         System.out.println("hello quartz...");
    13     }
    14 }
    HelloServiceImpl.java

    spring核心配置文件applicationContext.xml

     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" 
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xsi:schemaLocation="
     6         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
     8 
     9     <context:component-scan base-package="cn.itcast"></context:component-scan>
    10     <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
    11         <property name="jobClass" value="cn.itcast.quartz.HelloJob"></property>
    12     </bean>
    13     
    14     <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
    15         <property name="jobDetail" ref="jobDetail"></property>
    16         <property name="startDelay" value="3000"></property>
    17         <property name="repeatInterval" value="5000"></property>
    18     </bean>
    19     
    20     <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    21         <property name="triggers">
    22             <list>
    23                 <ref bean="simpleTrigger"/>
    24             </list>
    25         </property>
    26     </bean>
    27 </beans>
    applicationContext.xml

    描述:按上面配置的代码执行时,helloServiceImpl对象会无法注入,会报空指针异常。

    原因:因为JobDetailFactoryBean中注入的是一个cn.itcast.quartz.HelloJob实现类的全路径,底层会反射创建出一个HelloJob的对象,但是该对象不是由spring管理的,所以业务层的对象无法注入。

    解决方案有如下两种

    方案一:将如下类JobFactory复制放到自己项目下,然后修改配置文件,并将该JobFactory配置到applicationContext.xml中(详见配置文件第20行和第22行),helloServiceImpl就能够被注入了。

     1 package cn.itcast.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.AdaptableJobFactory;
     7 import org.springframework.stereotype.Service;
     8 
     9 @Service("jobFactory")
    10 public class JobFactory extends AdaptableJobFactory {
    11     @Autowired
    12     private AutowireCapableBeanFactory capableBeanFactory;
    13     @Override
    14     protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
    15         // 调用父类的方法
    16         Object jobInstance = super.createJobInstance(bundle);
    17         // 进行注入
    18         capableBeanFactory.autowireBean(jobInstance);
    19         return jobInstance;
    20     }
    21 }
    JobFactory.java
     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" 
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xsi:schemaLocation="
     6         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
     8 
     9     <context:component-scan base-package="cn.itcast"></context:component-scan>
    10     <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
    11         <property name="jobClass" value="cn.itcast.quartz.HelloJob"></property>
    12     </bean>
    13     
    14     <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
    15         <property name="jobDetail" ref="jobDetail"></property>
    16         <property name="startDelay" value="3000"></property>
    17         <property name="repeatInterval" value="5000"></property>
    18     </bean>
    19     
    20     <bean id="jobFactory" class="cn.itcast.quartz.JobFactory"></bean>
    21     <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    22         <property name="jobFactory" ref="jobFactory"></property>
    23         <property name="triggers">
    24             <list>
    25                 <ref bean="simpleTrigger"/>
    26             </list>
    27         </property>
    28     </bean>
    29 </beans>
    applicationContext.xml

    方案二:在Job实现类中添加下面这行代码即可(这种方式虽然方便,但是当你的Job实现类过多时,需要给每个类都添加该行代码,所以当Job实现类过多的时候建议还是采用方案一,只需要配置一次就OK)

    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

     1 package cn.itcast.quartz;
     2 
     3 import org.quartz.Job;
     4 import org.quartz.JobExecutionContext;
     5 import org.quartz.JobExecutionException;
     6 import org.springframework.beans.factory.annotation.Autowired;
     7 import org.springframework.stereotype.Service;
     8 import org.springframework.web.context.support.SpringBeanAutowiringSupport;
     9 
    10 import cn.itcast.service.HelloServiceImpl;
    11 
    12 /**      
    13  * @author: 攻城狮小白
    14  * @creationTime:2017年11月20日 下午5:21:28
    15  */
    16 @Service
    17 public class HelloJob implements Job{
    18     @Autowired
    19     private HelloServiceImpl helloServiceImpl;
    20     public void execute(JobExecutionContext context) throws JobExecutionException {
    21         SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    22         helloServiceImpl.sayHello();
    23     }
    24 }
    HelloJob.java
    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-5 06:03 , Processed in 0.058775 second(s), 28 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表