详情见:http://blog.csdn.net/wxwzy738/article/details/25158787

spring.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:task="http://www.springframework.org/schema/task"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  10. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
  11. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  12. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  13. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
  14. <task:annotation-driven /> <!-- 定时器开关-->
  15. <bean id="myTaskXml" class="com.spring.task.MyTaskXml"></bean>
  16. <task:scheduled-tasks>
  17. <!--
  18. 这里表示的是每隔五秒执行一次
  19. -->
  20. <task:scheduled ref="myTaskXml" method="show" cron="*/5 * * * * ?" />
  21. <task:scheduled ref="myTaskXml" method="print" cron="*/10 * * * * ?"/>
  22. </task:scheduled-tasks>
  23. <!-- 自动扫描的包名 -->
  24. <context:component-scan base-package="com.spring.task" />
  25. </beans>

=========================================两种方式:

01:基于注解

    利用spring中的以下配置

<task:annotation-driven /> <!-- 定时器开关-->

  1. <!-- 自动扫描的包名 -->
  2. <context:component-scan base-package="com.spring.task" />
  3. 类注解:
    1. @Component
  4. 方法注解:
  5. @Scheduled(cron = "0 0 1 * * *")

----------------------------------------------------------------------------------

  1. package com.spring.task;
  2. import org.springframework.scheduling.annotation.Scheduled;
  3. import org.springframework.stereotype.Component;
  4. /**
  5. * 基于注解的定时器
  6. * @author hj
  7. */
  8. @Component
  9. public class MyTaskAnnotation {
  10. /**
  11. * 定时计算。每天凌晨 01:00 执行一次
  12. */
  13. @Scheduled(cron = "0 0 1 * * *")
  14. public void show(){
  15. System.out.println("Annotation:is show run");
  16. }
  17. /**
  18. * 心跳更新。启动时执行一次,之后每隔2秒执行一次
  19. */
  20. @Scheduled(fixedRate = 1000*2)
  21. public void print(){
  22. System.out.println("Annotation:print run");
  23. }
  24. }

02:基于xml

    利用spring中的以下配置

  1. <task:scheduled-tasks>
  2. <!--
  3. 这里表示的是每隔五秒执行一次
  4. -->
  5. <task:scheduled ref="myTaskXml" method="show" cron="*/5 * * * * ?" />
  6. <task:scheduled ref="myTaskXml" method="print" cron="*/10 * * * * ?"/>
  7. </task:scheduled-tasks>
  1. package com.spring.task;
  2. /**
  3. * 基于xml的定时器
  4. * @author hj
  5. */
  6. public class MyTaskXml {
  7. public void show(){
  8. System.out.println("XMl:is show run");
  9. }
  10. public void print(){
  11. System.out.println("XMl:print run");
  12. }
  13. }

随机推荐

  1. Graph Cuts初步理解

    一些知识点的初步理解_8(Graph Cuts,ing...) Graph cuts是一种十分有用和流行的能量优化算法,在计算机视觉领域普遍应用于前背景分割(Image segmentation).立 ...

  2. Unity学习疑问记录之图片画质

    http://blog.csdn.net/candycat1992/article/details/22794773

  3. Visual Studio 2010编译时总是提示"调用目标发生了异常"的解决

    现象: 无论建立的是Win32 Console的解决方案,还是MFC的解决方案,重新打开Visual Studio 2010之后,编译时总是提示“调用的目标发生了异常” 解决: 1. 关闭Visual ...

  4. java设计模式

    五种创建型模式: 1.工厂模式 普通工厂模式: 工厂类提供一个方法可以生产多种实现了某种接口的类 多方法工厂模式: 一个方法对应一个要生产的类 静态工厂模式: 静态方法来生产类 2.抽象工厂模式 工厂 ...

  5. 将bean转换成键值列表

    日常开发中在进行接口对接的数据传输时,有一种场景是将bean转成jsonString,这里可以将bean转换成Map再转成jsonString. 工具类如下: public static String ...

  6. myBatis的一对多查询,主要利用resultMap实现一次查询多个结果集

    日常开发中有这中场景,一个用户有多个角色,一个角色又有多个菜单,想查出一个用户的所有菜单.除了常见的关联查询之外,更使用的应该是利用myBatis的resultMap来实现一次查询出多个结果集,缺点: ...

  7. 一起来做webgame,《卡片魔兽》(一)基础战斗

    写在前面的话 这不是教程,只是博主在娱乐过程中的一些小结记录.博主水平有限,没有什么高级的东西,只是将一些小的知识点结合一下,做这么一个养成类型的卡片页面游戏(=.=!有点绕).做一个完整的游戏,涉及 ...

  8. 贪吃蛇的java代码分析(一)

    自我审视 最近自己学习java已经有了一个多月的时间,从一开始对变量常量的概念一无所知,到现在能勉强写几个小程序玩玩,已经有了长足的进步.今天没有去学习,学校里要进行毕业答辩和拍毕业照了,于是请了几天 ...

  9. iOS移动硬盘实现原理

    iOS移动硬盘,仅仅是一个概念的包装,原理是在用户目录下创建一个特定的目录,应用层做一个界面直接访问这个目录而已. 大概流程是: 监听获得device句柄. AMDeviceConnect连接设备,并 ...

  10. Photosohp 2017 已经发布!(下载地址及破解方法在文章底部)

    Adobe Creative Cloud 软件于2016.11.2 全面更新,Adobe Creative Cloud 2017 震撼登场 全新的 2017版本,包含 Photoshop.Illust ...