spring注解scheduled實現定時任務


只想說,spring注解scheduled實現定時任務使用真的非常簡單。

一、配置spring.xml文件

  1、在beans加入xmlns:task="http://www.springframework.org/schema/task"以及在xsi:schemaLocation中加入

  http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd

  2、task任務掃描注解<task:annotation-driven />

  3、配置掃描的位置 <context:component-scan base-package="com.test"/>  

二、java代碼的實現

1 @Component// 實現定時任務的類必須被@Component注解
2 public class TestScheduled {
3 
4     @Scheduled(cron = "0 1 * * * ? ")
5     public void test() {// 定時器的任務方法不能有返回值
6         System.out.println("每分鍾執行一次");
7     }
8 }

三、運行查看結果

   當然是每分鍾打印一次嘍!

是不是很簡單的呀

 


免責聲明!

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



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