spring quartz定时 注入失败

<?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:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
 
<!--每天12:10分执行 0 10 12 * * ? * -->
<!--每天1点执行 0 0 1 * * ? * -->
<!--每天5秒执行一次 0/5 * * * * ? -->
<!--每天1点执行 0 13 14 * * ? * -->
<!-- 一、开启注释扫描 -->
<!-- <tx:annotation-driven />-->
<context:component-scan base-package="com.newtv.timer" />
 
<!--配置作业类-->
<bean name="job1" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.newtv.timer.Job1" />
<property name="jobDataAsMap"><!-- 非常重要,用来向JobDetail传参 -->
<map>
<entry key ="controller" value-ref="controller"/>
<entry key="timeout" value="0" />
</map>
</property>
 
<!-- <property name="jobClass" value="com.newtv.timer.Job1" />
<property name="jobDataAsMap">
<map>
<entry key="timeout" value="0" />
</map>
</property> -->
</bean>
 
<!-- 每2秒运行一次 -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="job1"/>
<property name="startDelay" value="0"/><!-- 调度工厂实例化后,经过0秒开始执行调度 -->
<property name="repeatInterval" value="2000"/><!-- 每2秒调度一次 -->
</bean>
 
<!-- 每30秒运行一次 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="job1" />
<property name="cronExpression" value="0 33 * * * ?" />
</bean>
 
<!-- 总调度,用于启动定时器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
 
</beans>
 
 
注意:使用spring quartz定时器,容易造成任务类中的注解注入失败,service等对象为null,原因是job是在quartz中实例化出来的,不受spring的管理。所以就导致注入不进去了。
posted @ 2018-04-30 21:41  kingdaqi  阅读(414)  评论(0编辑  收藏  举报