详情见: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. linux软件包管理(上)

    1.二进制包管理(RPM,yum) 2.源代码包的安装 3.脚本安装(shell或java) 4.Debian系列的linux软件包管理简介 在下载rmp包的时候注意检查硬件平台是否正确,如果硬件平台 ...

  2. 【emWin】例程一:emWin系列教程简介

    实验指导书及代码包下载: http://pan.baidu.com/s/1bpKbPBx

  3. spring装配---处理自动装配的歧义性

    一.歧义性 当我们使用spring的注解进行自动装配bean时,如果不仅有一个bean能够匹配结果的话,会抛出NoUniqueBeanDefinitionException: 例如本例中 当sprin ...

  4. 掌握Thinkphp3.2.0----模版基础

    我们将要学习一下 ThinkPHP 模版,ThinkPHP 内置了一个基于 XML 的性能卓越的模版引擎 ThinkTemplate,使用了动态编译和缓存技术,支持自定义标签库, 其实我还体会不到其中 ...

  5. Linux DNS配置

    1.安装bind #安装bind yum install -y bind bind-chroot bind-utils 2.主配置文件 vi /etc/named.conf #修改监听为本机IP li ...

  6. CSP -- 运营商内容劫持(广告)的终结者

    缘由 我们公司最近手机端H5 经常受到商户和用户的投诉,说有广告并且导致不能正常进行操作,我们商户自己当然不会加广告了,但是商户和用户可不管这些了,就认为是我们的问题 探索发现根本 目前我们用的很多浏 ...

  7. How to inspect who is caller of func and who is the class of instance

    1. Who is the class of self instance ? class aa(object): def a(self): if self.__class__.__name__ == ...

  8. Android Studio踩坑记

    1. 模拟器在管理器中可以运行,但是点击run时死活找不到:    在sdk的/platform-tools/目录下找到adb.exe工具    打开cmd,输入下面两条命令    adb kill- ...

  9. Oracle之ORDER BY

    ------------基本查询--1.查询出的是一张虚拟的结果表-----基本语法---- * 所有列(字段)select * from emps; -----查询指定字段select employ ...

  10. http://www.cnblogs.com/Lawson/archive/2012/09/03/2669122.html

    http://www.cnblogs.com/Lawson/archive/2012/09/03/2669122.html