spring自帶定時器@Scheduled注解配置


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


免責聲明!

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



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