http://www.cnblogs.com/pengmengnan/p/6714203.html

注解模式的spring定时器
1 , 首先要配置我们的spring.xml
xmlns 多加下面的内容、
xmlns:task="http://www.springframework.org/schema/task 

然后xsi:schemaLocation多加下面的内容、
1. http://www.springframework.org/schema/task 
2. 
http://www.springframework.org/schema/task/spring- 
task-3.1.xsd 
2,task任务扫描注解
<task:annotation-driven/>
3,配置扫描位置是:
<context:annotation-config/> 
<bean 
class="org.springframework.beans.factory.annotation.Au 
towiredAnnotationBeanPostProcessor"/> 
<context:component-scan base- 
package="com.jk.spring"/> 
4 ,写一个接口
public interface SpringHorodateurI {
public void myTest(); 
}
5,接口实现类
//import 
org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;

@Component //import 
org.springframework.stereotype.Component;
public class SpringHorodateur implements 
SpringHorodateurI{

@Scheduled(cron="0/5 * * * * ? ") //每5秒执行一 
次 
@Override 
public void myTest() {
System.out.println(">>>>>>>>>>>>>进入测 
试!!");
}

}

完成!
需要注意的几点:
1、spring的@Scheduled注解 需要写在实现上、
2、 定时器的任务方法不能有返回值(如果有返回值,spring初始 
化的时候会告诉你有个错误、需要设定一个proxytargetclass的 
某个值为true、具体就去百度google吧)
3、实现类上要有组件的注解@Component

配置文件格式的定时器
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/c 
ontext" 

xmlns:aop="http://www.springframework.org/schema/aop" 

xsi:schemaLocation="http://www.springframework.org/sch 
ema/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的定时器任务
/** 
* 基于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"); 

}

CRON表达式 含义 
"0 0 12 * * ?" 每天中午十二点触发 
"0 15 10 ? * *" 每天早上10:15触发 
"0 15 10 * * ?" 每天早上10:15触发 
"0 15 10 * * ? *" 每天早上10:15触发 
"0 15 10 * * ? 2005" 2005年的每天早上10:15触发 
"0 * 14 * * ?" 每天从下午2点开始到2点59分每分钟一次触 
发 
"0 0/5 14 * * ?" 每天从下午2点开始到2:55分结束每5分钟 
一次触发 
"0 0/5 14,18 * * ?" 每天的下午2点至2:55和6点至6点55 
分两个时间段内每5分钟一次触发 
"0 0-5 14 * * ?" 每天14:00至14:05每分钟一次触发 
"0 10,44 14 ? 3 WED" 三月的每周三的14:10和14:44触发 
"0 15 10 ? * MON-FRI" 每个周一、周二、周三、周四、周五 
的10:15触发

spring自带定时器的更多相关文章

  1. Spring 自带的定时任务

    需要几天后,或者某个时间后,定时查询数据.需要用到Spring自带的一个注解 @Scheduled(cron="0/5 * * * * ? ")//每隔5秒钟执行 创建一个clas ...

  2. spring 缓存(spring自带Cache)(入门)源码解读

    spring自带的缓存类有两个基础类:Cache(org.springframework.cache.Cache)类,CacheManager(org.springframework.cache.Ca ...

  3. spring 缓存(spring自带Cache)(入门)

    spring的缓存机制,是方法纬度的缓存机制, 这就意味着我们并不用关注 底层是否使用了数据库以及通过什么方式访问的数据库: 因此,此缓存方法既适用于dao层,也适用于service层. spring ...

  4. Spring的quartz定时器同一时刻重复执行二次的问题解决

    最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的hashcode,发现是不一样的,也就是说,在web容器启动的时候, ...

  5. 关于Spring的Quartz定时器设定

    在实际的开发业务中经常会遇到定时执行某个任务,如果项目使用的ssh框架的话,就需要配合spring来使用定时器.spring的定时器是一个固定的配置格式,具体的applicationContext配置 ...

  6. spring自带测试配置

    spring自带的测试注解 @ContextConfiguration(locations="classpath:applicationContext.xml")@RunWith( ...

  7. spring cron表达式(定时器)

    转: spring cron表达式(定时器) 写定时器时用到,记录一下: Cron表达式是一个字符串,字符串以5或6个空格隔开,分开工6或7个域,每一个域代表一个含义,Cron有如下两种语法 格式:  ...

  8. Spring的quartz定时器重复执行二次的问题解决

    Spring的quartz定时器同一时刻重复执行二次的问题解决 最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的ha ...

  9. spring 自带框架及可替换框架

    spring 自带框架 可替换框架 (可替换框架)是否推荐使用 spring security shiro 推荐使用 spring aop aspectj 集成aspectj使用 Shiro 对比 S ...

随机推荐

  1. intelij Android 搭建 java 项目

    1.打开ide创建页面,点击创建项目 2.选择Android,发现下面有一段红字,解释很清楚,由于第一使用为设置Android的开发环境sdk,去设置一下就好,点击取消回到上一界面 3 配置 4.当然 ...

  2. SuperSocket 最基础入门

    ---恢复内容开始--- SuperSocket 是什么? 首先我们明确一下SuperSocket 本质是什么? 网络框架 !  ok , 那么我们直接上上官网,作者已经开源到Github,可以做两件 ...

  3. bzoj 2752: [HAOI2012]高速公路(road)

    Description Y901高速公路是一条重要的交通纽带,政府部门建设初期的投入以及使用期间的养护费用都不低,因此政府在这条高速公路上设立了许多收费站.Y901高速公路是一条由N-1段路以及N个收 ...

  4. js知识点记录

    1.for...in:用于遍历数组或对象的属性. for(var prop in obj){console.log(obj[prop])}; 该循环遍历对于对象属性是无序的,可能是因为obj本来就是无 ...

  5. 访问vm中centos的web站点

    vm网络连接设置成NAT 需要把centos设置成静态IP 再不行,记得把centos的防火墙先关闭

  6. COMPUTE子句和Group By

                        首先声明一下,这个COMPUTE语法在SQLServer 2012之后,就废弃使用了,详情请看https://msdn.microsoft.com/librar ...

  7. BCL和CoreFx的区别

    bcl是.netframework clr 的基础库corefx是.net core clr的基础库

  8. 房上的猫:if选择结构

    一.基本if结构: 1.定义:if选择结构是根据条件判断之后再做处理的一种语法结构! 2.逻辑:首先对条件进行判断  >如果为真,则执行代码块  >如果为假,执行代码块后面的部分二.常用逻 ...

  9. yield next和yield* next的区别

    yield next和yield* next之间到底有什么区别?为什么需要yield* next?经常会有人提出这个问题.虽然我们在代码中会尽量避免使用yield* next以减少新用户的疑惑,但还是 ...

  10. 第四章:大数据 の HBase 基础

    本课主题 NoSQL 数据库介绍 HBase 基本操作 HBase 集群架构与设计介紹 HBase 与HDFS的关系 HBase 数据拆分和紧缩 引言 介绍什么是 NoSQL,NoSQL 和 RDBM ...