Spring Task提供两种方式进行配置,一种是annotation(标注),而另外一种就是XML配置了。因为任务调度这样的需求,通常改动都是比较多的,如果用annotation的方式的话,改动就变得麻烦了,必须去重新编译。所以,我只是选择用XML配置的方式。
1.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:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <bean id="reminderProcessor" class="com.foo.task.ReminderProcessor"> <property name="workers"> <array value-type="com.foo.task.Worker"> <ref bean="projectScheduleRemindWorker" /> </array> </property> </bean> <!-- 配置任务线性池 --> <task:executor id="executor" pool-size="3" /> <task:scheduler id="scheduler" pool-size="3" /> <!-- 启用annotation方式 --> <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true" /> <task:scheduled-tasks scheduler="scheduler"> <task:scheduled ref="reminderProcessor" method="process" cron="0 0 12 * * ?" /> </task:scheduled-tasks> </beans>
2.注解
<?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:sec="http://www.springframework.org/schema/security" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"> <!—spring扫描注解的配置 --> <context:component-scan base-package="com.test.cache" /> <!-- 配置任务线性池 --> <task:scheduler id="scheduler" pool-size="10" /> <task:executor id="executor" pool-size="10" /> <!—开启这个配置,spring才能识别@Scheduled注解 --> <task:annotation-driven scheduler="scheduler" executor="executor" /> </beans>
java的类
package com.test.cache; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class CrontabTask { @Scheduled(cron = "0 0/5 * * * *") public void autoBid() { System.out.println("执行定时任务"); } }
参考: http://zywang.iteye.com/blog/949123