scheduled註解做定時任務正解(無坑)

2021-09-24 12:43:11 字數 1086 閱讀 7765

如何使用schedule來做定時任務:

1.首先在springmvc.xml配置檔案上加上約束文件:

要注意新增的時候要對應好,不然解析的時候會出現問題。2.開啟註解配置,掃瞄對應的類,開啟定時任務,配置任務排程器

這裡配置容易出現的問題

configuration problem: only one asyncannotationbeanpostprocessor may exist within the context.

這是因為定時配置重複了,把開啟定時的那一行刪去就好了

3.注釋設定定時任務

@scheduled(cron = "0/2 * * * * ?")

public void getdata()

注意這個類一定要被springmvc那個掃瞄能掃到啊!出現問題:發現定時器沒有執行,需要請求才能執行:這是因為servlet採取懶載入機制,只有在呼叫的時候才會去載入。解決辦法:修改web.xml

在建立servlet的過程中加上設定load-on-startup>=1;

springmvc

org.springframework.web.servlet.dispatcherservlet

contextconfiglocation

classpath:config/springmvc.xml

1

ok,解決問題。

定時任務註解 Scheduled

scheduled cron 0 0 2 每天凌晨兩點執行 void dosomethingwith 乙個cron表示式有至少6個 也可能7個 有空格分隔的時間元素。按順序依次為 秒 0 59 分鐘 0 59 小時 0 23 天 月 0 31,但是你需要考慮你月的天數 月 0 11 天 星期 1 7...

回顧定時任務註解 Scheduled

scheduled註解是包org.springframework.scheduling.annotation中的乙個註解,主要是用來開啟定時任務。target retention retentionpolicy.runtime documented repeatable schedules.clas...

Scheduled 定時任務總結

scheduled 作用 spring定時器 定時執行一次或定時輪詢執行一段 使用場景 註解在方法上 引數說明 常用引數 scheduled 引數說明 string cron cron表示式定義了方法執行的時間規則 網上對這個的說明很多就不墨跡了 生成器工具位址 例子 scheduled fixed...