詳解在Spring3中使用注解(@Scheduled)創(chuàng)建計劃任務(wù)
Spring3中加強了注解的使用,其中計劃任務(wù)也得到了增強,現(xiàn)在創(chuàng)建一個計劃任務(wù)只需要兩步就完成了:
- 創(chuàng)建一個Java類,添加一個無參無返回值的方法,在方法上用@Scheduled注解修飾一下;
- 在Spring配置文件中添加三個<task:**** />節(jié)點;
最后說明一下,第一步創(chuàng)建的Java類要成為spring可管理的Bean,可以直接寫在XML里,也可以@Component一下
示例如下
計劃任務(wù)類:
/** * com.zywang.spring.task.SpringTaskDemo.java * @author ZYWANG 2011-3-9 */ package com.zywang.spring.task; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * Spring3 @Scheduled 演示 * @author ZYWANG 2011-3-9 */ @Component public class SpringTaskDemo { @Scheduled(fixedDelay = 5000) void doSomethingWithDelay(){ System.out.println("I'm doing with delay now!"); } @Scheduled(fixedRate = 5000) void doSomethingWithRate(){ System.out.println("I'm doing with rate now!"); } @Scheduled(cron = "0/5 * * * * *") void doSomethingWith(){ System.out.println("I'm doing with cron now!"); } }
Spring配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!-- Enables the Spring Task @Scheduled programming model --> <task:executor id="executor" pool-size="5" /> <task:scheduler id="scheduler" pool-size="10" /> <task:annotation-driven executor="executor" scheduler="scheduler" /> </beans>
以上內(nèi)容基于Spring 3.0.5 版本運行,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Spring自帶定時任務(wù)@Scheduled注解實例講解
- Spring定時任務(wù)@scheduled多線程使用@Async注解示例
- Spring定時任務(wù)@Scheduled注解(cron表達式fixedRate?fixedDelay)
- Spring中的@Scheduled定時任務(wù)注解詳解
- SpringBoot中@Scheduled()注解以及cron表達式詳解
- Spring 定時任務(wù)@Scheduled 注解中的 Cron 表達式詳解
- SpringBoot中定時任務(wù)@Scheduled注解的使用解讀
- spring-boot通過@Scheduled配置定時任務(wù)及定時任務(wù)@Scheduled注解的方法
- spring @Scheduled定時任務(wù)注解使用方法及注意事項小結(jié)
相關(guān)文章
Intellij IDEA中一次性折疊所有Java代碼的快捷鍵設(shè)置
這篇文章主要介紹了Intellij IDEA中一次性折疊所有Java代碼的快捷鍵設(shè)置,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05Java基礎(chǔ)之static關(guān)鍵字的使用講解
這篇文章主要介紹了Java基礎(chǔ)之static關(guān)鍵字的使用講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07解決Springboot @Autowired 無法注入問題
WebappApplication 一定要在包的最外層,否則Spring無法對所有的類進行托管,會造成@Autowired 無法注入。接下來給大家介紹解決Springboot @Autowired 無法注入問題,感興趣的朋友一起看看吧2018-08-08tk.mybatis通用插件updateByPrimaryKeySelective無法自動更新列的解決辦法
tk.mybatis是一個很好用的通用插件,本文主要介紹了tk.mybatis通用插件updateByPrimaryKeySelective無法自動更新列的解決辦法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12