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 ...
随机推荐
- SuperSocket 最基础入门
---恢复内容开始--- SuperSocket 是什么? 首先我们明确一下SuperSocket 本质是什么? 网络框架 ! ok , 那么我们直接上上官网,作者已经开源到Github,可以做两件 ...
- Servlet与Jsp的结合使用实现信息管理系统一
PS:1:先介绍一下什么是Servlet? Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地 ...
- Tablayout ViewPage 使用示例
上一篇文章介绍了使用 FragmenttabHost 来使用 tab 导航:到 Android 5.0 的时候,又推出了 TabLayout.因此,有必要对tablayout 进行了解下. 首先我们来 ...
- python sklearn PCA源码阅读:参数n_components的设置(设为‘mle’出错的原因)
在介绍n_components参数之前,首先贴一篇PCA参数详解的文章:http://www.cnblogs.com/akrusher/articles/6442549.html. 按照文章中对于n_ ...
- MySQL优化五 SQL优化
1.减少 IO 次数 IO永远是数据库最容易瓶颈的地方,这是由数据库的职责所决定的,大部分数据库操作中超过90%的时间都是 IO 操作所占用的,减少 IO 次数是 SQL 优化中需要第一优先考虑,当然 ...
- 如何在Spring框架上做开发之Context启动中的“Hook”
1.概述 有些时候,我们需要在spring启动过程中加入一些自己的逻辑,特别是一些基本框架和spring做整合的时候(例如:mybatis-spring-boot-starter),就需要使用Spri ...
- linux 常见操作指令
1.ssh root@ip ssh 登录 2.ll ls 列出当文件夹下 所以文件 3. cd ./xx 进入 xx 文件夹 4. vim vi xxx 进入 xx文件的 编辑模式. i 开始编辑 e ...
- 【转】Appium的安装-Mac平台(命令行 dmg)
其实Appium的安装方式主要有两种: 1)自己安装配置nodejs的环境,然后通过npm进行appium的安装 2)直接下载官网提供的dmg进行安装,dmg里面已经有nodejs的环境和appium ...
- 【Tarjan】洛谷P3379 Tarjan求LCA
题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...
- 【数论·错位排列】bzoj4517 排列计数
4517: [Sdoi2016]排列计数 Time Limit: 60 Sec Memory Limit: 128 MBSubmit: 1428 Solved: 872[Submit][Statu ...