spring task 實現定時執行(補充:解決定時任務執行2次問題)


首先在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 的解決方案)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM