日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring3.0设置定时任务

發布時間:2025/7/14 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring3.0设置定时任务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天做個小需求,需要用到定時器。

就把以前寫過的配置文件模板直接復制過來,又順手點進去看了一下源碼,發現TimerFactoryBean、ScheduledTimerTask都已經被標記成@Deprecated了
Java代碼 ?
  • @Deprecated??
  • public?class?TimerFactoryBean?implements?FactoryBean<Timer>,?BeanNameAware,?InitializingBean,?DisposableBean??
  • @Deprecated public class TimerFactoryBean implements FactoryBean<Timer>, BeanNameAware, InitializingBean, DisposableBean
    Java代碼 ?
  • @Deprecated??
  • public?class?ScheduledTimerTask??
  • @Deprecated public class ScheduledTimerTask
    那肯定就不樂意用了,就上網找了找spring3.0之后的新用法,果然是有變化,而且比以前簡單了很多,在這里記錄一下

    我記得以前那種做法,業務類是要繼承自TimerTask才行的,現在就不用了,是一個pojo就可以
    Java代碼 ?
  • public?class?TestService?{ ??
  • ??
  • ????private?Logger?logger?=?LoggerFactory.getLogger(TestService.class); ??
  • ??
  • ????public?void?sayHello()?{ ??
  • ????????System.out.println("hello?world"); ??
  • ????} ??
  • ??
  • ????public?void?sayBye()?{ ??
  • ????????System.out.println("bye?world"); ??
  • ????} ??
  • ??
  • }??
  • public class TestService {private Logger logger = LoggerFactory.getLogger(TestService.class);public void sayHello() {System.out.println("hello world");}public void sayBye() {System.out.println("bye world");}}
    然后配置文件也更簡單
    Xml代碼 ?
  • <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">??
  • ??
  • ????<bean?id="testService"?class="com.xxx.spring.business.TestService"?/>??
  • ??
  • ????<task:scheduled-tasks>??
  • ????????<task:scheduled?ref="testService"?method="sayHello"?cron="3/11?*?*?*?*??"?/>??
  • ????????<task:scheduled?ref="testService"?method="sayBye"?cron="7/13?*?*?*?*??"?/>??
  • ????</task:scheduled-tasks>??
  • ??
  • </beans>??
  • <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.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"><bean id="testService" class="com.xxx.spring.business.TestService" /><task:scheduled-tasks><task:scheduled ref="testService" method="sayHello" cron="3/11 * * * * ?" /><task:scheduled ref="testService" method="sayBye" cron="7/13 * * * * ?" /></task:scheduled-tasks></beans>


    只要用一個新增的<task:scheduled-tasks>就可以了

    就是有一點要注意一下,新的時間配置,是類似于cron的語法,比以前強大很多。

    不過我只用到了第一個參數:3/11,表示延遲3秒啟動,間隔11秒;7/13表示延遲7秒啟動,間隔13秒

    ?

    轉自:http://www.iteye.com/topic/1125517

    總結

    以上是生活随笔為你收集整理的spring3.0设置定时任务的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。