task

http://blog.csdn.net/xwygn/article/details/8440692

首先要配置咱們的spring.xmlhtml


xmlns 多加下面的內容、java

[html]  view plain copy
  1. xmlns:task="http://www.springframework.org/schema/task"  


而後xsi:schemaLocation多加下面的內容、spring

[html]  view plain copy
  1. http://www.springframework.org/schema/task  
  2. http://www.springframework.org/schema/task/spring-task-3.1.xsd  

最後是咱們的task任務掃描註解

[html]  view plain copy
  1. <task:annotation-driven/>  

個人配置掃描位置是:

[html]  view plain copy
  1. <context:annotation-config/>  
  2.     <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  
  3.     <context:component-scan base-package="com.test"/>  


掃描的是com.test這樣的包下的內容、

下面須要接口和實現(個人這幾個java文件都是com.test的包下的、)app

[java]  view plain copy
  1. public interface IMyTestService {  
  2.        public void myTest();  
  3. }  



[java]  view plain copy
  1. @Component  //import org.springframework.stereotype.Component;  
  2. public class MyTestServiceImpl  implements IMyTestService {  
  3.       @Scheduled(cron="0/5 * *  * * ? ")   //每5秒執行一次  
  4.       @Override  
  5.       public void myTest(){  
  6.             System.out.println("進入測試");  
  7.       }  
  8. }  

執行後控制檯就會打印出   進入測試   了


須要注意的幾點:ide

一、spring的@Scheduled註解  須要寫在實現上、oop

二、 定時器的任務方法不能有返回值(若是有返回值,spring初始化的時候會告訴你有個錯誤、須要設定一個proxytargetclass的某個值爲true、具體就去百度google吧)測試

三、實現類上要有組件的註解@Componentgoogle


剩下的就是corn表達式了、具體使用以及參數請百度google、url

下面只例出幾個式子spa

CRON表達式    含義 
"0 0 12 * * ?"    天天中午十二點觸發 
"0 15 10 ? * *"    天天早上10:15觸發 
"0 15 10 * * ?"    天天早上10:15觸發 
"0 15 10 * * ? *"    天天早上10:15觸發 
"0 15 10 * * ? 2005"    2005年的天天早上10:15觸發 
"0 * 14 * * ?"    天天從下午2點開始到2點59分每分鐘一次觸發 
"0 0/5 14 * * ?"    天天從下午2點開始到2:55分結束每5分鐘一次觸發 
"0 0/5 14,18 * * ?"    天天的下午2點至2:55和6點至6點55分兩個時間段內每5分鐘一次觸發 
"0 0-5 14 * * ?"    天天14:00至14:05每分鐘一次觸發 
"0 10,44 14 ? 3 WED"    三月的每週三的14:10和14:44觸發 
"0 15 10 ? * MON-FRI"    每一個周1、周2、周3、周4、週五的10:15觸發 




相關文章
相關標籤/搜索