spring+quartz实现定时任务遇到问题总结(bean无法注入)

spring+quartz实现定时任务遇到问题总结

近期项目需要用到定时任务。每隔一定时间查询订单的交易状态,经过查询决定用spring+quartz来实现

主要分为三个步骤:

1.编写定时任务的执行类和执行方法

2.配置定时任务类和执行方法

3.启动服务器执行定时任务

1.编写定时类和执行方法:

public class TestTask {
    
    private static Logger logger = Logger.getLogger(TestTask.class);
    
    @Autowired
    public OrderService orderService;
    
    public void execute(){
        logger.debug("执行了------TestTask execute log--------");
    }
    
}

2.配置定时任务类和执行方法:配置文件:spring-quartz.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

     <!-- 请自行忽略下面这一行-->
    <context:component-scan base-package="com.xxx.rechargems.app" />
     
    <!-- 配置定时任务类 -->
    <bean id="testTask" class="com.xxx.rechargems.app.task.TestTask" />
    <bean id="testJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- targetObject用于指定定时任务的类 -->
        <property name="targetObject" ref="testTask"/>
        <!--指定目标封装为有状态的任务,有状态的任务不能并发执行,无状态的任务可并发执行-->
        <property name="concurrent" value="false" />
        <!-- targetMethod用于指定定时任务的类中需要执行的方法 -->
        <property name="targetMethod" value="execute"/>
    </bean>
    <bean id="testCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
        <property name="jobDetail" ref="testJob" />
        <!-- 延时启动时间,单位ms -->
        <property name="startDelay" value="0" />
        <!-- cron表达式,此处是每分钟执行一次 -->
        <property name="cronExpression" value="0 * * * * ?" />
    </bean>
    
    <bean id="testTask2" class="com.xxx.rechargems.app.task.TestTask2" />
    <bean id="testJob2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="testTask2"/>
        <property name="concurrent" value="false" />
        <property name="targetMethod" value="execute"/>
    </bean>
    
    <bean id="testCronTrigger2" class="org.springframework.scheduling.quartz.SimpleTriggerBean">  
        <property name="jobDetail" ref="testJob2" />  
        <!-- 调度工厂实例化后,经过0秒开始执行调度   -->
        <property name="startDelay" value="0" />
        <!-- 每2秒调度一次   -->
        <property name="repeatInterval" value="2000" />
    </bean>
   
    <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        
        <property name="triggers">  
            <list>
                <ref bean="testCronTrigger"/>
                <ref bean="testCronTrigger2"/>
            </list>  
        </property>
    </bean>

</beans>

3.启动服务器执行定时任务。

至此应该是定时任务启动,皆大欢喜了,但是,刺激的来了,启动报错!!!!!!!!!!!!!!!!!!!!!!

看了下报错,是bean没有注入!!!

     @Autowired
    public OrderService orderService;

就是这段导致的,于是找原因:

1.jar包:quartz-all.jar包已经配置(pom已经田间相应的依赖配置)

2.配置文件是否已经被读取:web.xml中已经添加了spring-quartz.xml:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mybatis.xml,classpath:spring-quartz.xml</param-value>

</context-param>

3.tomcat和jdk版本:原来是jdk1.7和tomcat7,但是spring是4.0版本,为了查看是否为版本问题,就改为jdk1.8和tomcat-8.0,但是仍然报错。

无奈ing。。。。。。。。。。。。。。。。。。。。。。。。。

最后原因是没有添加扫描包!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

在:spring-mybatis.xml或者spring-quartz.xml中添加扫描包路径即可:<context:component-scan base-package="com.xxx.rechargems.app" />

至此问题解决。

springmvc中配置了注解扫描,但是spring的注解和spring的注解扫描应该各自配置,只配置springmvc的扫描,spring注解是无法使用的,这个大概是造成不能注入的原因!!!至于是否是这个原因有待验证!!!!


前期问题:配置了quartz,没有添加service注入时,定时任务打死不执行,后来发现是spring启动时没有找到定时的相关配置,即没有初始化quartz,应该把spring-quartz.xml

添加至spring初始化配置在中去,就是原因2中的那段配置,

解决办法最终由尤总发现,在此感谢!!!!


猜你喜欢

转载自blog.csdn.net/zwq3210/article/details/73498031