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. windows下 安装 rabbitMQ 及操作常用命令(操作创建用户密码 角色等)

    rabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统.它遵循Mozilla Public License开源协议,采用 Erlang 实现的工业级的消息队列(MQ)服务器,Rab ...

  2. 447. Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  3. ArcGIS 网络分析[8.6] 资料6 创建网络分析图层及进行路径分析

    基于上篇所介绍的内容,就说说如何利用访问到的网络数据集,在Map中添加网络数据集图层.创建网络分析图层中的路径图层,并执行路径分析示例.

  4. 重写JS的鼠标右键点击菜单

    重写JS的鼠标右键点击菜单 该效果主要有三点,一是对重写的下拉菜单的隐藏和显示:二是屏蔽默认的鼠标右键事件:三是鼠标左键点击页面下拉菜单隐藏. 不多说,上html代码: 1 <ul id=&qu ...

  5. 关于模拟登陆微博(PC)

    微博模拟登陆 1.基类对象的方法建立一个类__init__初始化方法,接收username和password. class launcher(): def __init__(self, usernam ...

  6. 《吸血鬼日记》(The Vampire Diaries)经典台词

    Best quotes from The Vampire Diary 1. I will start fresh, be someone new. 1.我要重新开始,做不一样的自己. 2. It’s ...

  7. Excel数据导入至Dataset中

    public static DataSet ExcelToDataSet(string ppfilenameurl,string pptable) { string strConn = "P ...

  8. 商业智能(BI)选型手册(转载)

    摘自http://articles.e-works.net.cn/bi/Article126429.htm 1.前言 互联网时代企业数据呈现爆发式增长,全面考验着企业的数据处理和分析能力.面对大容量. ...

  9. 对vuex的认识和简单理解

    vuex概述 Vuex 是一个主要应用在中大型单页应用的类似于 Flux 的数据管理架构.它主要帮我们更好地组织代码,以及把应用内的的状态保持在可维护.可理解的状态. 但如果是简单的应用 ,就没有必要 ...

  10. npm安装删除模块以及cnpm淘宝镜像

    npm安装模块 [$ npm install xxx]利用 npm 安装xxx模块到当前命令行所在目录: [$ npm install -g xxx]利用npm安装全局模块xxx: npm 删除模块 ...