在spring配置文件applicationContent.xml中添加以下内容
1) xml表头中添加task命名空间
xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"2)设置自动扫描,通过注解来扫描相应的的bean
<!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。 --> <context:component-scan base-package="com.jeeplus"><!-- base-package 如果多个,用“,”分隔 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> 3)配置计划任务 <!-- 计划任务配置,用 @Service @Lazy(false)标注类,用@Scheduled(cron = "0 0 2 * * ?")标注方法 --> <task:executor id="executor" pool-size="10"/> <task:scheduler id="scheduler" pool-size="10"/> <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/> 4)java建立对应的方法 @Controller public class TimingUtil { @Autowired private ZjkyBcOrderService zjkyBcOrderService; @Scheduled(cron="0 0 0 * * ?") //每天零点执行 public void shenheTiming(){ System.out.println("定时"); } } 注意,这里的方法不能有返回值。
在spring配置文件applicationContent.xml中添加以下内容
<!-- ***********Quartz 作业调度************** --> <!-- 要调用的工作类,指向所要调用的action --> <bean id="QuartzJob" class="com.ctfo.module.Traffic.action.TrafficAction"></bean> <!-- 定义调用对象和调用对象的方法 --> <bean id="jobtask1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 调用的类 --> <property name="targetObject"> <!-- ref标签中为bean的id,指向某个class --> <ref bean="QuartzJob"/> </property> <!-- 调用类中的方法 --> <property name="targetMethod"> <!-- 指向方法名 --> <value>faSongXiaoXi</value> </property> </bean> <!-- 定义触发时间 --> <bean id="doTime1" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <!-- 指向所要执行的触发器--> <ref bean="jobtask1"/> </property> <!-- cron表达式 --> <property name="cronExpression"> <value>0/30 * * * * ?</value> </property> </bean> <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --> <bean id="startQuertz1" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="doTime1"/> </list> </property> </bean>