Spring注解配置定時任務


http://m.blog.csdn.net/article/details?id=50945311

 

首先在配置文件頭部的必須要有:
xmlns:task="http://www.springframework.org/schema/task"
1
其次xsi:schemaLocation必須為其添加:
http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task.xsd
1
2
然后spring掃描過程必須涵蓋定時任務類所在的目錄:
<context:component-scan base-package="com.xx.xx" />
1
com.xx.xx屬於定時任務類的父級甚至更高級 
然后設置動作啟用定時任務
<task:annotation-driven/>
1
最后設置任務類
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 = "*/5 * * * * ?")//每隔5秒執行一次
    public void test() throws Exception {
        System.out.println("Test is working......");
    }


    //@Scheduled(cron = "0 0 1 * * ?")//每天凌晨1點整
    //@Scheduled(cron = "0 30 0 * * ?")//每天凌晨0點30分
    //@Scheduled(cron = "0 */60 * * * ?")//1小時處理一次
}
 

 


免責聲明!

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



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