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

Job实现类代码

package cn.itcast.service;

import org.springframework.stereotype.Service;


@Service
public class HelloServiceImpl {
    public void sayHello(){
        System.out.println("hello quartz...");
    }
}

HelloServiceImpl.java

spring核心配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="cn.itcast"></context:component-scan>
    <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="cn.itcast.quartz.HelloJob"></property>
    </bean>
    
    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="jobDetail"></property>
        <property name="startDelay" value="3000"></property>
        <property name="repeatInterval" value="5000"></property>
    </bean>
    
    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="simpleTrigger"/>
            </list>
        </property>
    </bean>
</beans>

applicationContext.xml

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

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

解决方案有如下两种

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

package cn.itcast.quartz;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Service;

@Service("jobFactory")
public class JobFactory extends AdaptableJobFactory {
    @Autowired
    private AutowireCapableBeanFactory capableBeanFactory;
    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        // 调用父类的方法
        Object jobInstance = super.createJobInstance(bundle);
        // 进行注入
        capableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
    }
}

JobFactory.java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="cn.itcast"></context:component-scan>
    <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="cn.itcast.quartz.HelloJob"></property>
    </bean>
    
    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="jobDetail"></property>
        <property name="startDelay" value="3000"></property>
        <property name="repeatInterval" value="5000"></property>
    </bean>
    
    <bean id="jobFactory" class="cn.itcast.quartz.JobFactory"></bean>
    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobFactory" ref="jobFactory"></property>
        <property name="triggers">
            <list>
                <ref bean="simpleTrigger"/>
            </list>
        </property>
    </bean>
</beans>

applicationContext.xml

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

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

package cn.itcast.quartz;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import cn.itcast.service.HelloServiceImpl;


@Service
public class HelloJob implements Job{
    @Autowired
    private HelloServiceImpl helloServiceImpl;
    public void execute(JobExecutionContext context) throws JobExecutionException {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
        helloServiceImpl.sayHello();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值