今天做个小需求,需要用到定时器。

就把以前写过的配置文件模板直接复制过来,又顺手点进去看了一下源码,发现TimerFactoryBean、ScheduledTimerTask都已经被标记成@Deprecated了

Java代码  
  1. @Deprecated
  2. public class TimerFactoryBean implements FactoryBean<Timer>, BeanNameAware, InitializingBean, DisposableBean
@Deprecated
public class TimerFactoryBean implements FactoryBean<Timer>, BeanNameAware, InitializingBean, DisposableBean
Java代码  
  1. @Deprecated
  2. public class ScheduledTimerTask
@Deprecated
public class ScheduledTimerTask

那肯定就不乐意用了,就上网找了找spring3.0之后的新用法,果然是有变化,而且比以前简单了很多,在这里记录一下

我记得以前那种做法,业务类是要继承自TimerTask才行的,现在就不用了,是一个pojo就可以

Java代码  
  1. public class TestService {
  2. private Logger logger = LoggerFactory.getLogger(TestService.class);
  3. public void sayHello() {
  4. System.out.println("hello world");
  5. }
  6. public void sayBye() {
  7. System.out.println("bye world");
  8. }
  9. }
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代码  
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans.xsd
  5. http://www.springframework.org/schema/task
  6. http://www.springframework.org/schema/task/spring-task-3.0.xsd">
  7. <bean id="testService" class="com.xxx.spring.business.TestService" />
  8. <task:scheduled-tasks>
  9. <task:scheduled ref="testService" method="sayHello" cron="3/11 * * * * ?" />
  10. <task:scheduled ref="testService" method="sayBye" cron="7/13 * * * * ?" />
  11. </task:scheduled-tasks>
  12. </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.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>

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

就是有一点要注意一下,新的时间配置,是类似于cron的语法,比以前强大很多。

不过我只用到了第一个参数:3/11,表示延迟3秒启动,间隔11秒;7/13表示延迟7秒启动,间隔13秒

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

spring3.0设置定时任务相关推荐

  1. ppst技术视频——spring3.0 注解定时任务配置

    ppst 视频-- spring3.0 注解定时任务配置:请访问ppst 技术视频分享平台 , www.ppst.cc,上面有最新的技术视频 1.在spring的配置文件中加入配置 <conte ...

  2. linux设置定时备份任务,Linux下Oracle设置定时任务备份数据库的教程

    1.查看数据库的字符集 数据库的字符集必须和linux下设置的环境变量一致,不然会有乱码. 以下两个sql语句都可以查到: select * from nls_database_parameters ...

  3. 使用Spring3+Quartz实现定时任务

    在本文中,我们使用Spring3+Quartz实现定时任务.spring中定义了一些接口来支持Quartz,可以使你的业务逻辑类和Quartz解耦. 这个例子中使用了如下maven的依赖项: < ...

  4. oracle定时任务可以备份么,Linux下Oracle设置定时任务备份数据库的教程

    1.查看数据库的字符集 数据库的字符集必须和Linux下设置的环境变量一致,不然会有乱码. 以下两个sql语句都可以查到: select * from nls_database_parameters ...

  5. spring怎么设置定时任务为每天凌晨2点执行和每小时执行一次?(亲测)

    每天凌晨2点  0 0 2 * * ?和每天隔一小时 0 * */1 * * ? 例1:每隔5秒执行一次:*/5 * * * * ? 例2:每隔5分执行一次:0 */5 * * * ? 在26分.29 ...

  6. 使用shell脚本调用mysql数据库存储过程,并设置定时任务

    本来是要mysql数据库中创建事件任务来,定时执行存储过程,做数据传输的...后来由于种种原因,就使用crontab来定时执行,调用存储过程. 实现这个数据传输分为两步: 第一步:编写shell脚本调 ...

  7. spring3.0异步_在Spring 4.2中更简单地处理异步事务绑定事件

    spring3.0异步 介绍 如您可能已经知道的(例如,从我以前的博客文章中 ),不再需要创建一个单独的类来实现带有onApplicationEvent方法的ApplicationListener , ...

  8. Maven整合Spring3.0+Mybatis3.2+Struts2.3+查找坐标+jar包依赖(五)

    依赖传递 只添加了一个struts2-core依赖,发现项目中出现了很多jar,这种情况 叫 依赖传递 查找坐标 依赖版本冲突的解决 1.  第一声明优先原则 <dependencies> ...

  9. 边做边学小型封装--利用主流框架进行Dao层通用化,Spring3.0+Hibernate3.3.2通用Dao层整合(四)...

    了解Dao层的创建和HibernateSupportDao后,忘了要把Spring3.0的Jar包加进去lib文件夹里面,由于Spring3.0开始没有把所有需要有关联的Jar包跟功能整合包放在一起, ...

最新文章

  1. static关键字用法
  2. python中的time库安装步骤-Python time库基本操作方法
  3. python备份发包脚本_Python备份脚本,python
  4. pmp每日三题(2022年2月22日)
  5. 卓越性能代码_装好win10后,应该这样设置,才能压榨出系统十足的性能
  6. mediumtext和string转换_数据库用varchar和text的差别
  7. 11.python并发入门(part5 event对象)
  8. 【MySql】mysql 慢日志查询工具之mysqldumpslow
  9. android usb存储固定,如何在Android 10中设置默认USB行为
  10. django之orm的高级操作以及xcc安全攻击
  11. ETHREAD APC 《寒江独钓》内核学习笔记(4)
  12. 构造方法与构造代码块的区别
  13. 《GO语言圣经》学习初感与建议
  14. matlab调取excel非线性拟合,用matlab实现非线性曲线拟合_matlab非线性曲线拟合
  15. python 基因测序_使用机器学习和Python揭开DNA测序神秘面纱
  16. 根据广播星历计算GNSS卫星在瞬时地球坐标系中的坐标
  17. 【计算机网络】互联网上的音频/视频服务
  18. Android 8.0 状态栏信号显示、信号定制
  19. Response.WriteFile 无法下载大文件解决方法
  20. Bsp开发的几个层次

热门文章

  1. 过去可忆,未来可期(随心录+杂记)
  2. JUC多线程核心知识-思维导图
  3. 第九次会议(5.14)
  4. mysql中RAND()随便查询记录效率问题和解决的方法分享
  5. Ubuntu系统如何卸载并安装新版本的jdk(permission denied问题)
  6. 架构编译器框架系统 LLVM 使用简介
  7. 普中开发仪 HC6800EM3-v22光盘资料
  8. 买房猛于虎《功夫熊猫无家可归》
  9. luogu4677山区建小学题解--区间DP
  10. python---django中orm的使用(5)数据库的基本操作(性能相关:select_related,和prefetch_related重点)(以及事务操作)...