1、spring的配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <task:annotation-driven /> <!-- 定时器开关--> <bean id="myTaskXml" class="com.spring.task.MyTaskXml"></bean> <task:scheduled-tasks>
<!-- 这里表示的是每隔五秒执行一次 -->
<task:scheduled ref="myTaskXml" method="show" cron="*/5 * * * * ?" />
<task:scheduled ref="myTaskXml" method="print" cron="*/10 * * * * ?"/>
</task:scheduled-tasks> <!-- 自动扫描的包名 -->
<context:component-scan base-package="com.spring.task" /> </beans>

2、基于xml的定时器任务

package com.spring.task;  

/**
* 基于xml的定时器
* @author hj
*/
public class MyTaskXml { public void show(){
System.out.println("XMl:is show run");
} public void print(){
System.out.println("XMl:print run");
}
}

3、基于注解的定时器任务

    package com.spring.task;  

    import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; /**
* 基于注解的定时器
* @author hj
*/
@Component
public class MyTaskAnnotation { /**
* 定时计算。每天凌晨 01:00 执行一次
*/
@Scheduled(cron = "0 0 1 * * *")
public void show(){
System.out.println("Annotation:is show run");
} /**
* 心跳更新。启动时执行一次,之后每隔2秒执行一次
*/
@Scheduled(fixedRate = 1000*2)
public void print(){
System.out.println("Annotation:print run");
}
}

4、测试

package com.spring.test;  

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mvc.xml");
}
}

运行结果:

Annotation:print run

Annotation:print run

Annotation:print run

XMl:print run

XMl:is show run

Annotation:print run

Annotation:print run

spring定时任务执行两次bug参考链接:http://nkliuliu.iteye.com/blog/816335

转载链接:http://blog.csdn.net/wxwzy738/article/details/25158787

spring自带的定时任务功能,基于注解和xml配置的更多相关文章

  1. 8 -- 深入使用Spring -- 4...5 AOP代理:基于注解的“零配置”方式

    8.4.5 基于注解的“零配置”方式 AspectJ允许使用注解定义切面.切入点和增强处理,而Spring框架则可识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5 一样的 ...

  2. spring自带的定时任务功能@EnableScheduling

    1 demo package com.test.domi.config; import org.springframework.beans.factory.annotation.Configurabl ...

  3. 8 -- 深入使用Spring -- 4...6 AOP代理:基于注解的XML配置文件的管理方式

    8.4.6 基于XML配置文件的管理方式 Spring 2.x 提供一个新的aop:命名空间来定义切面.切入点和增强处理. XML配置方式优点: ⊙ 如果应用没有使用JDK 1.5 以上版本,那么应用 ...

  4. spring框架学习(二)使用注解代替xml配置

    注解 1.使用注解配置spring 1)开启使用注解代理配置文件 <?xml version="1.0" encoding="UTF-8"?> &l ...

  5. Spring基于注解和XML混合方式的使用

    首先要明白,基于注解和XML两种方式的实现功能是一样的,只是两种不同的配置方式. 一.IoC配置 1.配置xml 在使用注解与xml结合的方式配置IoC之前,首先要引入context标签: xmlns ...

  6. 10 Spring框架--基于注解的IOC配置

    1.工程环境搭建 2.基于注解的IOC配置 IOC注解的分类 (1)用于创建对象的 他们的作用就和在XML配置文件中编写一个<bean>标签实现的功能是一样的@Component: 作用: ...

  7. spring-第十七篇之spring AOP基于注解的零配置方式

    1.基于注解的零配置方式 Aspect允许使用注解定义切面.切入点和增强处理,spring框架可以识别并根据这些注解来生成AOP代理.spring只是用了和AspectJ 5一样的注解,但并没有使用A ...

  8. Spring声明式事务管理(基于注解方式实现)

    ----------------------siwuxie095                                 Spring 声明式事务管理(基于注解方式实现)         以转 ...

  9. Spring自带的定时任务框架Schedule的优缺点及使用

    spring自带的定时任务框架的有点:简单,拆箱即用 spring自带的定时任务框架的缺点: 不支持集群:为避免重复执行的问题 不支持生命周期统一管理:不重启服务情况下关闭,启动任务 不支持分片任务: ...

随机推荐

  1. 优秀的linux学习网站

    从网络上拷贝别人归纳的列表. Linux优秀网站列表 国内 http://www.chinaunix.net/ 国内最火爆的unix/linux论坛 http://www.linuxforum.net ...

  2. RGB转到HSV色彩空间转换

    原文链接:https://blog.csdn.net/lsg19920625/article/details/78416649

  3. (递归)Hanoi Tower

    #include<stdio.h>void move(int n,char a,char b){ printf("将第%d个盘子从%c移动到%c\n",n,a,b);  ...

  4. HDU - 1150 Machine Schedule(二分匹配---最小点覆盖)

    题意:有两台机器A和B,A有n种工作模式(0~n-1),B有m种工作模式(0~m-1),两台机器的初始状态都是在工作模式0处.现在有k(0~k-1)个工作,(i,x,y)表示编号为i的工作可以通过机器 ...

  5. 从AppleWatch4发布后对手股价大跌看可穿戴市场未来

    万众瞩目的苹果秋季发布会终于落下了帷幕,这场发布会既有惊喜,也有遗憾.遗憾的是新款iPad Pro.廉价版Macbook air没有亮相.iPhone系列价格较贵等,惊喜的则是iPhone的处理器依然 ...

  6. VC6.0 The value of ESP was not properly saved across a function call 错误解决方法

    调用DLL函数,出现错误 Run-Time Check Failure #0 - The value of ESP was not properly saved across a function c ...

  7. Docker安装 - CentOS7环境

    Docker安装 - CentOS7环境 安装Docker 我是虚拟机装的Centos7,linux 3.10 内核,docker官方说至少3.8以上,建议3.10以上(ubuntu下要linux内核 ...

  8. visualVM远程监控jetty

    1.打开本体bin/visualvm 2.添加远程主机 3.启动应用,使用以下方式 java -Djava.rmi.server.hostname=远程IP地址 -Dcom.sun.managemen ...

  9. [CISCN2019 华北赛区 Day1 Web1]Dropbox-phar文件能够上传到服务器端实现任意文件读取

    0x00知识点 phar是什么: 我们先来了解一下流包装 大多数PHP文件操作允许使用各种URL协议去访问文件路径:如data://,zlib://或php://.例如常见的 include('php ...

  10. 和我一起从0学算法(C语言版)(二)

    第一章 排序 第三节 快速排序 快速排序是最常用的排序方法.快排运用的递归方法很有意思.掌握了这种排序方法可以在将来学习递归时更快入门.只是快排的思路与之前的排序方法相比较为复杂,再加担心上我的表达能 ...