详情见: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. php多线程详解

    在说明多线程的题前,需要弄清楚以下几个问题 1,ts 和 nts的区别 Thread Safe和NoneThread Safe 先说windows的,在php官网,在windows区域有在文件下在有 ...

  2. Thread-Safe Resource Manager

    http://php.net/manual/en/internals2.memory.tsrm.php When PHP is built with Thread Safety enabled, th ...

  3. 深入理解ANGULARUI路由_UI-ROUTER

    最近在用 ionic写个webapp 看到几个demo中路由有好几种,搞的有点晕,查下资料研究下,做个笔记,其中大部分为摘抄别人的,做个说明免得被人吐槽. Angularjs ui-router - ...

  4. argparse解析参数模块

    一.简介: argparse是python用于解析命令行参数和选项的标准模块,用于代替已经过时的optparse模块.argparse模块的作用是用于解析命令行参数,例如python parseTes ...

  5. ruby -检查数据类型

    HashObj={","language"=>"zh","make"=>"Apple"," ...

  6. Creating Signing Identities 生成签名标识

    Before you can code sign your app, you create your development certificate and later, a distribution ...

  7. Windows Server 2008 R2组策略创建用户桌面快捷方式

    问题: 如何让所有域用户桌面有一个公司共享的快捷方式,让所有域用户直接双击就能打开公司共享. 解决办法: 1.创建一个zhuyu组织单元 ----- 在zhuyu组织单元创建一个域用户user1. 2 ...

  8. 安装redis

    第一步 下载 第二步 解压 .tar.gz 第三步 make cd redis- make 第四步  启动试一下 src/redis-server 好了 :C Jan ::13.501 # Warni ...

  9. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.0 版本新增序列管理

    欲了解V3.0版本的相关内容可查看下面的链接地址. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.0 版本发布 在V3.0版本的Web(Mvc.WebForm)与WinF ...

  10. DataTable 转 List<T>

    最近在做一个项目,表的数据巨多,而且表的字段一般都在30个以上.公司规定不能用Nhibernate以及ef等ORM框架. 所以查询绑定时的工作量极为痛苦.没有办法,自己写了个DataTableToLi ...