詳解在Spring3中使用注解(@Scheduled)創(chuàng)建計(jì)劃任務(wù)
Spring3中加強(qiáng)了注解的使用,其中計(jì)劃任務(wù)也得到了增強(qiáng),現(xiàn)在創(chuàng)建一個(gè)計(jì)劃任務(wù)只需要兩步就完成了:
- 創(chuàng)建一個(gè)Java類,添加一個(gè)無(wú)參無(wú)返回值的方法,在方法上用@Scheduled注解修飾一下;
- 在Spring配置文件中添加三個(gè)<task:**** />節(jié)點(diǎn);
最后說(shuō)明一下,第一步創(chuàng)建的Java類要成為spring可管理的Bean,可以直接寫在XML里,也可以@Component一下
示例如下
計(jì)劃任務(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 版本運(yùn)行,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring自帶定時(shí)任務(wù)@Scheduled注解實(shí)例講解
- Spring定時(shí)任務(wù)@scheduled多線程使用@Async注解示例
- Spring定時(shí)任務(wù)@Scheduled注解(cron表達(dá)式fixedRate?fixedDelay)
- Spring中的@Scheduled定時(shí)任務(wù)注解詳解
- SpringBoot中@Scheduled()注解以及cron表達(dá)式詳解
- Spring 定時(shí)任務(wù)@Scheduled 注解中的 Cron 表達(dá)式詳解
- SpringBoot中定時(shí)任務(wù)@Scheduled注解的使用解讀
- spring-boot通過(guò)@Scheduled配置定時(shí)任務(wù)及定時(shí)任務(wù)@Scheduled注解的方法
- spring @Scheduled定時(shí)任務(wù)注解使用方法及注意事項(xiàng)小結(jié)
相關(guān)文章
Intellij IDEA中一次性折疊所有Java代碼的快捷鍵設(shè)置
這篇文章主要介紹了Intellij IDEA中一次性折疊所有Java代碼的快捷鍵設(shè)置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Java基礎(chǔ)之static關(guān)鍵字的使用講解
這篇文章主要介紹了Java基礎(chǔ)之static關(guān)鍵字的使用講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07SpringBoot集成JWT令牌詳細(xì)說(shuō)明
這篇文章主要介紹了SpringBoot集成JWT令牌詳細(xì)說(shuō)明,JWT方式校驗(yàn)方式更加簡(jiǎn)單便捷化,無(wú)需通過(guò)redis緩存,而是直接根據(jù)token取出保存的用戶信息,以及對(duì)token可用性校驗(yàn),單點(diǎn)登錄,驗(yàn)證token更為簡(jiǎn)單,需要的朋友可以參考下2023-10-10解決Springboot @Autowired 無(wú)法注入問(wèn)題
WebappApplication 一定要在包的最外層,否則Spring無(wú)法對(duì)所有的類進(jìn)行托管,會(huì)造成@Autowired 無(wú)法注入。接下來(lái)給大家介紹解決Springboot @Autowired 無(wú)法注入問(wèn)題,感興趣的朋友一起看看吧2018-08-08tk.mybatis通用插件updateByPrimaryKeySelective無(wú)法自動(dòng)更新列的解決辦法
tk.mybatis是一個(gè)很好用的通用插件,本文主要介紹了tk.mybatis通用插件updateByPrimaryKeySelective無(wú)法自動(dòng)更新列的解決辦法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12