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)注解