基於Spring task註解方式配置任務

 此文標題有錯,感謝各位網友指出

        林炳文Evankaka原創作品。轉載請註明出處http://blog.csdn.net/evankaka

        新建一個Java工程,導入要用到的包,Spring3.2、Quartz2.2.1、aopalliance-1.0.jar、commons-logging-1.2.jar。整個工程目錄如下:


本文工程免費下載

1、配置需要調度的類,並添加註解
[java] view plain copy
  1. package com.mucfc;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Date;  
  4.   
  5. import org.springframework.scheduling.annotation.Scheduled;  
  6. import org.springframework.stereotype.Component;  
  7. /**   
  8. *事件類,基於Spring註解方式 
  9. *作者 林炳文([email protected] 博客:http://blog.csdn.net/evankaka)   
  10. *時間 2015.4.29 
  11. */  
  12. @Component  
  13. public class MyJob {  
  14.     public MyJob(){  
  15.         System.out.println("MyJob創建成功");  
  16.     }  
  17.      @Scheduled(cron = "0/1 * *  * * ? ")//每隔1秒隔行一次  
  18.         public void run(){  
  19.          System.out.println("Hello MyJob  "+  
  20.                     new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date()));  
  21.         }  
  22.   
  23. }  
2、首先要配置我們的beans.xml,在xmlns 多加下面的內容
[html] view plain copy
  1. xmlns:task="http://www.springframework.org/schema/task"  
3、然後xsi:schemaLocation多加下面的內容
[java] view plain copy
  1. http://www.springframework.org/schema/task    
  2. http://www.springframework.org/schema/task/spring-task-3.0.xsd  

4、最後是我們的task任務掃描註解

[java] view plain copy
  1. <!--開啓這個配置,spring才能識別@Scheduled註解-->  
  2.  <task:annotation-driven/>   
5、自動配置掃描位置:

[java] view plain copy
  1. <!-- 自動掃描註解的bean -->  
  2. lt;context:component-scan base-package="com.mucfc"/>  
6、整個文檔如下
[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.     http://www.springframework.org/schema/context     
  8.     http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  9.     http://www.springframework.org/schema/task    
  10.     http://www.springframework.org/schema/task/spring-task-3.0.xsd">  
  11.   
  12.     <!--開啓這個配置,spring才能識別@Scheduled註解-->  
  13.      <task:annotation-driven/>  
  14.      <!-- 自動掃描註解的bean -->  
  15.     <context:component-scan base-package="com.mucfc"/>  
  16.   
  17. </beans>  
7、使用
[java] view plain copy
  1. package com.mucfc;  
  2. import org.springframework.context.ApplicationContext;  
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4. public class Test {  
  5.   
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");  
  9.     }  
  10.   
  11. }  

輸出結果:


一旦這個xml被加載進來來,就會自動創建bean的實例,並且開始定時任務了
需要注意的幾點:
1、spring的@Scheduled註解  需要寫在實現上
2、 定時器的任務方法不能有返回值(如果有返回值,spring初始化的時候會告訴你有個錯誤、需要設定一個proxytargetclass的某個值爲true、具體就去百度google吧)
3、實現類上要有組件的註解@Component

林炳文Evankaka原創作品。轉載請註明出處http://blog.csdn.net/evankaka

本文工程免費下載

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章