1.仅需引入spring相关的包。
2.在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.1.xsd
3.配置定时任务的线程池
<task:annotation-driven scheduler="myScheduler" mode="proxy" />
<task:scheduler id="myScheduler" pool-size="10"/>
4.写定时任务
package com.cn.springmvc.controller;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @author wyy
* @date 2017年4月7日
*/
@Component
@Lazy(false)
@EnableScheduling
public class TaskJob {
@Scheduled(cron = "0/5 * * * * *")
public void job1() {
System.out.println("任务进行中。。。");
}
}
总结:
1.配置定时任务线程池可以同时执行同一时间的任务,否则是按照顺序执行。
2.如果xml里面开启的懒加载,default-lazy-init="true",需要有@Lazy(false)注解