详情见: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. SQL21日自学通笔记(1)

    查找不重复数据 DISTINCT 查询日期 在access中格式是yyyy-mm-dd,Query执行的语句中用‘#’+Formatdata(‘yyyy-mm-dd’,date)+‘#’ SQL运算符 ...

  2. 标签案例-开发foreach标签

    if(item.getClass().isArray()){ this.collection = new ArrayList(); int length = Array.getLength(items ...

  3. MongoDB的配置、启动、关闭

    MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系 ...

  4. KnockoutJS:

    一.ko对象 js对象的改变都会导致viewmodel的变化,但view不一定变化 往ko对象里面push,viewmodel的变化,引起view的变化. 往js对象里面push,model的变化引起 ...

  5. Java将文件转为字节数组

    Java将文件转为字节数组 关键字:文件,文件流,字节流,字节数组,二进制 摘要:最近工作中碰到的需求是,利用http传输二进制数据到服务器对应接口,需要传输userId, file(加密后)等一系列 ...

  6. JavaScript + PHP 实现刷新继续保持倒计时的按钮

    场景:发送一个验证码到手机,当验证码发出时,会提示隔 1 分钟之后可以再次发送.通常有这几种方式防止恶意请求,一是再次发送之前需要输入验证码,二是在指定的时间间隔之内不能再次发送. 有些网站在 1 分 ...

  7. vpn

    https://itunes.apple.com/us/app/sonicwall-mobile-connect/id822514576?mt=12

  8. Tiddlywiki 维基程序使用手册

    文档 http://tiddlywiki.com/ http://web.nlhs.tyc.edu.tw/~lss/wiki/TiddlyWikiTutorialTW.html https://sit ...

  9. MySQL运行状态show status详解

    状态名 作用域 详细解释 Aborted_clients Global 由于客户端没有正确关闭连接导致客户端终止而中断的连接数 Aborted_connects Global 试图连接到MySQL服务 ...

  10. 使用git@osc管理现有项目

    首先安装git和powershell集成git工具 参考 http://www.cnblogs.com/kreo/p/4685988.html 打开windows powershell,进入项目目录, ...