Quartz Job未实例化导致Spring @Autowired 注入为null

来源:互联网 发布:linux设置javahome 编辑:程序博客网 时间:2024/06/05 08:08

写在前面

上周组长布置给我一个任务,在 Java 多数据源的配置一文中也提到过,大概就是一个从别人的数据库中查询出自己需要的数据,然后存到本地的数据库中的流程。本以为就一个java多数据源的知识点,最后需求又更新为独立开发一个子项目来做这件事情,而子项目需要继承父项目,从这次的开发任务中,遇到很多困难,也学到很多知识。

遇到的问题

  • Spring @Autowired 注入为null
@Service  //这里的注入不知道是否必须要的呢?public class LargeTableFarTransferJobImpl implements Job{    @Autowired    private LargeTableFarTransferService largeTableFarTransferService;   //Bug:此处largeTableFarTransferService为null    @Override    @Transactional    public void execute(JobExecutionContext context) throws JobExecutionException {        Calendar nowC = Calendar.getInstance();        log.info("大表远传定时任务开始");        List<LargeTableFarTransferEntity> tableList = largeTableFarTransferService.getLargeTableList();        log.info("执行大表远传的定时任务,大表的数量是{}",tableList.size());        ...    }}

由于是注入的问题,开始以为是配置的问题,便询问了好多大神,自己也查了很多资料,反复检查Spring @Autowired 注入流程会涉及到的配置文件,还是没找到问题所在,无奈之下,求助于组长。组长先是看了一遍我的配置文件,再看了一下我敲的代码,也一直没发现问题,期间,我对定时任务的QuartzJobFacotry.java文件感到好奇,询问组长这是用来干嘛的,代码如下:

/** *  * @ClassName: QuartzJobFacotry  * @author: huangjp * @date: 2017年4月10日 下午6:49:23 */public class QuartzJobFacotry extends SpringBeanJobFactory{    @Autowired    private AutowireCapableBeanFactory beanFactory;    @Override    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {        Object jobInstance = super.createJobInstance(bundle);        beanFactory.autowireBean(jobInstance);        return jobInstance;    }}

组长大概介绍了一下,是用来实例化Job的什么的,巴拉巴拉,我大概理解了这个文件的意思(其实之前也大概猜到了=。=),虽然不是很清楚,但是我还是在项目中加上了这个文件,却忽略了对它的配置,说到这儿,组长也想到了我会不会是这儿配置的不对,检查了一下果然是配置有问题,代码如下:

<?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="com.wx.app.ygp.dbyc.task"/>    <bean id="jobInstance" class="com.wx.app.ygp.dbyc.task.jobs.QuartzJobFacotry"></bean>    <!-- 大表远传定时任务 -->    <bean id="largeTableFarTransferJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">        <property name="jobClass" value="com.wx.app.ygp.dbyc.task.jobs.LargeTableFarTransferJobImpl"></property>    </bean>    <!-- 大表远传定时任务的触发时间 -->    <bean id="largeTableFarTransferTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">        <property name="jobDetail" ref="largeTableFarTransferJob"></property>        <property name="cronExpression" value="0 */3 * * * ?"></property>    </bean>    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">        <property name="triggers">            <list>                <ref bean="largeTableFarTransferTriggerBean"/>            </list>        </property>        <property name="jobFactory" ref="jobInstance"/>  <!--这里缺少了配置,相当于有了一个名为jobInstance的bean,却没有用到它-->        <property name="quartzProperties">            <props>                <prop key="org.quartz.scheduler.instanceName">DBYC-QuartzScheduler</prop>            </props>        </property>    </bean></beans>

最终加上这句代码,问题就解决了。

结语

在这次的任务中,遇到的严重bug就这一个,卡了我很久,最终发现是因为自己的不仔细导致的,十分不应该,不过,经过这个bug让我学到了很多别的知识,比如说Spring @Autowired注入的配置流程以及与它相关的四个地方,还有对Spring quartz有了一个更深的认识等等。以后都会写相关的博客记载下来。

0 0
原创粉丝点击