spring自带定时器
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自带定时器的更多相关文章
- Spring 自带的定时任务
需要几天后,或者某个时间后,定时查询数据.需要用到Spring自带的一个注解 @Scheduled(cron="0/5 * * * * ? ")//每隔5秒钟执行 创建一个clas ...
- spring 缓存(spring自带Cache)(入门)源码解读
spring自带的缓存类有两个基础类:Cache(org.springframework.cache.Cache)类,CacheManager(org.springframework.cache.Ca ...
- spring 缓存(spring自带Cache)(入门)
spring的缓存机制,是方法纬度的缓存机制, 这就意味着我们并不用关注 底层是否使用了数据库以及通过什么方式访问的数据库: 因此,此缓存方法既适用于dao层,也适用于service层. spring ...
- Spring的quartz定时器同一时刻重复执行二次的问题解决
最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的hashcode,发现是不一样的,也就是说,在web容器启动的时候, ...
- 关于Spring的Quartz定时器设定
在实际的开发业务中经常会遇到定时执行某个任务,如果项目使用的ssh框架的话,就需要配合spring来使用定时器.spring的定时器是一个固定的配置格式,具体的applicationContext配置 ...
- spring自带测试配置
spring自带的测试注解 @ContextConfiguration(locations="classpath:applicationContext.xml")@RunWith( ...
- spring cron表达式(定时器)
转: spring cron表达式(定时器) 写定时器时用到,记录一下: Cron表达式是一个字符串,字符串以5或6个空格隔开,分开工6或7个域,每一个域代表一个含义,Cron有如下两种语法 格式: ...
- Spring的quartz定时器重复执行二次的问题解决
Spring的quartz定时器同一时刻重复执行二次的问题解决 最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的ha ...
- spring 自带框架及可替换框架
spring 自带框架 可替换框架 (可替换框架)是否推荐使用 spring security shiro 推荐使用 spring aop aspectj 集成aspectj使用 Shiro 对比 S ...
随机推荐
- HtmlTestRunner无法生成HTML报告问题
环境: Python3.6 + Selenium3.3.0 + HtmlTestRunner1.1.0 ON Windows10 IDE: PyCharm HtmlTestRunner地址: http ...
- shell 踩坑记
变量赋值时,等号两边不能有空格: 在判断表达式中,不论是 [ -n "$1" ] 还是 [ -f "$1" ] 都要在变量两侧加上双引号: 在使用与或非判断式 ...
- C/C++调用Golang 一
C/C++调用Golang 一 (开发环境: 操作系统: windows 7 32位操作系统 C++: visual studio 2010 Golang:go version go1.9 windo ...
- Java订单号生成,唯一订单号(日均千万级别不重复)
Java订单号生成,唯一订单号 相信大家都可以搜索到很多的订单的生成方式,不懂的直接百度.. 1.订单号需要具备以下几个特点. 1.1 全站唯一性. 1.2 最好可读性. 1.3 随机性,不能重复,同 ...
- 解决Windows和Linux使用npm打包js和css文件不同的问题
1.问题出现 最近公司上线前端H5页面,使用npm打包,特别奇怪的是每次打包发现css和js文件与Windows下打包不一致(网页使用Windows环境开发),导致前端页面功能不正常. 2.问题排查 ...
- python csv模块的reader是一个迭代器,无法多次迭代
在一个项目中,我需要多次遍历一个文本,该文本我是用csv.reader读取的.但后来发现,本文只对第一次循环有用,而之后的循环均为空白.经过排错后,我确定问题就出现在csv.reader()这一步.之 ...
- jquery获取select选中的值
http://blog.csdn.net/renzhenhuai/article/details/19569593 误区: 一直以为jquery获取select中option被选中的文本值,是这样写的 ...
- OA常见问题和解决方案
本文档:主要用来记录OA常见的问题和解决方案. (一)更新问题(登陆不了,或者登陆出错) 由于很多用户使用的是XP系统,导致每次进行OA进行升级的时候,他们都不支持自动升级.如果不支持自动升级的话,那 ...
- 前端学习_01_css网页布局
引子 之前也自己陆陆续续地学了一些web方面的知识,包括前段和后端都有涉及到,自己也比较感兴趣,感谢peter老师,愿意无偿提供从零开始的教学,之前也看过peter老师的一些视频,节奏非常适合我,决心 ...
- jQuery的get()post()getJson()方法
jQuery get() 和 post() 方法用于通过 HTTP GET 或 POST 请求从服务器请求数据. HTTP 请求:GET vs. POST 两种在客户端和服务器端进行请求-响应的常用方 ...