spring定時器用Annotation實現

2021-08-31 13:07:53 字數 1123 閱讀 5669

spring定時器用annotation實現

由於專案中需要定時更新一些資料,所以用到了spring的定時器,在使用quartz過程中,遇到了一些麻煩,最終牽扯的錯誤太多:

1、我的乙個service類中需要加入定時執行即service extends quartzjobbean,但是service類中使用@autowired注入了屬性:dao物件

2、在executeinternal方法執行過程中,dao物件一直為null。

3、嘗試了各種辦法,最終在配置檔案中這樣配置(這種配置網上多的是)

使用jobdataasmap這個屬性

org.wapgame.service.charts.service

4、executeinternal方法中使用其引數來獲得值。

protected void executeinternal(jobexecutioncontext arg0) throws jobexecutionexception

這樣就解決了注入的物件為null的問題。網上找了好多找不到解決的辦法,最終還得靠自己。

6、放棄,轉annotation

通過 註解 來排程任務

通過 配置檔案 來排程任務cron表示式去搜吧,好多,我搜到了一些,如下:

乙個cron表示式有到少6個(也可能是7個)由空格分隔的時間元素.從左到右,這些元素的定義如下: 

1.秒(0-59) 

2.分鐘(0-59) 

3.小時(0-23) 

4.月份中的是期(1-31) 

5.月份(1-12或sun-dec) 

6.星期中的日期(1-7或sun-sat) 

7.年份(1970-2099)  

例子: 

0 0 10,14,16 * * ? 每天上午10點,下午2點和下午4點 

0 0,15,30,45 * 1-10 * ? 每月前10天每隔15分鐘 

30 0 0 1 1 ? 2012 在2023年1月1日午夜過30秒時 

0 0 8-5 ? * mon-fri 每個工作日的工作時間 

- 區間 

* 萬用字元 

? 你不想設定那個字段 

spring定時器用Annotation實現

spring定時器用annotation實現 由於專案中需要定時更新一些資料,所以用到了spring的定時器,在使用quartz過程中,遇到了一些麻煩,最終牽扯的錯誤太多 1 我的乙個service類中需要加入定時執行即service extends quartzjobbean,但是service類...

spring定時器用Annotation實現

0人收藏此文章,我要收藏 發表於3個月前 已有46次閱讀 共0 a 需要在xmlns裡面加入 xmlns task b 在xsi schemalocation中加入 spring task 3.0.xsd 別忘了最後的引號 c 加入配置 d web.xml org.springframework.w...

MFC 定時器用法

mfc定時器的使用 1.在標頭檔案定義乙個afx msg void ontimer uint ptr nidevent 函式 定義乙個定時器id define systimetimer 1 2.實現該函式 void cminigatetes lg ontimer uint ptr nidevent ...