Spring3.0以后自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz
=========================
一、配置文件方式
1.任务执行类的编写
@Service
public class TaskJob {
public void job1() {
System.out.println("任务进行中。。。");
}
}
2.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<task:scheduled-tasks>
<task:scheduled ref="taskJob" method="job1" cron="0/1 * * * * ?" />
</task:scheduled-tasks>
</beans>
ref参数指定的即任务类,method指定的即需要运行的方法,cron及cronExpression表达式
二、注解方式
1.任务执行类的编写
@Service
public class TaskJob {
@Scheduled(cron = "0/1 * * * * ?")
public void job1() {
System.out.println("任务进行中。。。");
}
}
2.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!-- 开启这个配置,spring才能识别@Scheduled注解 -->
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/>
</beans>
整理自:http://gong1208.iteye.com/blog/1773177