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 ...
随机推荐
- 小白的 MySQL 笔记(一)
来自 stackoverflow 的内容居多. 1- MySQL VARCHAR size? 2- 数据库设计范式 3- What is InnoDB and MyISAM in MySQL ? 4- ...
- Who's in the Middle
FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'median' ...
- 佛祖保佑永无bug的源代码
${AnsiColor.BRIGHT_YELLOW} ${AnsiColor.BRIGHT_RED}_ooOoo_${AnsiColor.BRIGHT_YELLOW} ${AnsiColor.BRIG ...
- MySQL字符串相关函数学习二
① LOWER(str):将字符串转为小写:与此函数具有相同作用的函数有LCASE() 如果参数是小写.数字或其他特殊字符,则返回原数据 ② LEFT(str, len):返回字符串str左边的len ...
- python科学计算_scipy_常数与优化
scipy在numpy的基础上提供了众多的数学.科学以及工程计算中常用的模块:是强大的数值计算库: 1. 常数和特殊函数 scipy的constants模块包含了众多的物理常数: import sci ...
- windows64 系统下安装Nodejs
1.nodejs安装: 版本号: node-v8.9.3-x64 检测安装成功:(这个版本已经集成了npm) 2.npm作为一个NodeJS的模块管理,先配置npm的全局模块的存放路径以及cache的 ...
- 总结Oracle8i 的UNDO表空间损坏(ORA-01092及ORA-00600【4193】)情况下的数据库不完全恢复的经历
服务器断电重启导致备份生产环境的恢复目录库无法进行启动,提示Ora-01092例程终止.强行断开连接 查看跟踪日志: Wed Jan 10 08:41:37 2018 Errors in file d ...
- vmvare centos 7.0 root密码忘记后重置及总结
今天遇到了一个比较尴尬的事情,我centos 7.0的虚拟机密码忘了.....里面还有我配置好的环境呢.于是我就上网上搜索各种方法,最后经我验证下面这个方法比较靠谱: 使用光盘修复Centos: ht ...
- python之dictionary
1.python3字典 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 d ...
- asp.net mvc 防止重复提交
string hiddenToken = "hiddenToken"; public void OnActionExecuting(ActionExecutingContext f ...