首先在spring-mvc.xml配置頭文件引入:
xmlns:task="http://www.springframework.org/schema/task"
其次引入task對應的xsi:schemaLocation:
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
然后確保spring進行組件掃描時涵蓋定時任務類所在的包:
<context:component-scan base-package="xx.xx.xx" />
最后設置任務類即可:
import org.springframework.context.annotation.Lazy; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component @Lazy(value=false) public class MyQuartzs { @Scheduled(cron = "0 0 0 * * ?")//零點時執行一次 public void test() { //TODO } }
至於@Scheduled(cron="0 0 0 * * ?")中cron的意思,請看我的另一篇文章。
http://www.cnblogs.com/sprinkle/p/6440018.html
補充:解決定時任務執行2次問題
請看:
Spring3 Shedule Task之注解實現 (兩次啟動Schedule Task 的解決方案)