spring的自动定时任务有两种

第一种:通过xml配置来设置

需要在xml中引入新的约束,并且需要配置<task:scheduled-tasks> ,主要配置内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<context:component-scan base-package="com.shsxt.timer"/>
<task:scheduled-tasks>
<task:scheduled ref="timer" method="mobileCode" cron="0/2 * * * * ?"/>
</task:scheduled-tasks>
</beans>
<task:scheduled ref="timer" method="mobileCode" cron="0/2 * * * * ?"/>
ref的值等于类名的首字母小写,method的是类中的方法名 cron表达式表示定时多长时间,此时该值表示两秒,可以通过cron表达式在线生成器来获取定时任务对应的表达式

通过配置这些,该类中的方法的逻辑就可被定时的执行了

第二种通过注解来配置定时器,需要xml配置注解<task:annotation-driven>即可
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<context:component-scan base-package="com.shsxt.timer"/>
<task:annotation-driven></task:annotation-driven>
</beans>

在类中的方法上需要配置注解@Scheduled(cron =" 0/2 * * * * ?")

具体代码内容如下:

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import java.text.SimpleDateFormat;
import java.util.Date; @Component
public class Timer {
@Scheduled(cron = "0/2 * * * * ? ")
public void mobileCode(){
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" ).format(new Date()));
}
}

												

spring 的自动定时任务的更多相关文章

  1. Spring+Quartz 实现定时任务的配置方法

    Spring+Quartz 实现定时任务的配置方法 整体介绍 一.Quartz介绍 在企业应用中,我们经常会碰到时间任务调度的需求,比如每天凌晨生成前天报表,每小时生成一次汇总数据等等.Quartz是 ...

  2. Spring整合Quartz定时任务 在集群、分布式系统中的应用(Mysql数据库环境)

    Spring整合Quartz定时任务 在集群.分布式系统中的应用(Mysql数据库环境)   转载:http://www.cnblogs.com/jiafuwei/p/6145280.html 单个Q ...

  3. Eureka 系列(03)Spring Cloud 自动装配原理

    Eureka 系列(03)Spring Cloud 自动装配原理 [TOC] 0. Spring Cloud 系列目录 - Eureka 篇 本文主要是分析 Spring Cloud 是如何整合 Eu ...

  4. spring类型自动转换——@InitBinder和Converter

    spring有2种类型转换器,一种是propertyEditor,一种是Converter.虽然都是类型转换,但是还是有细微差别. 所以这里以一个例子的形式来分析一下这2种类型转换的使用场景和差别. ...

  5. 使用BeanNameAutoProxyCreator实现spring的自动代理

    提到代理,我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置 ...

  6. spring多线程与定时任务

    本篇主要描述一下spring的多线程的使用与定时任务的使用. 1.spring多线程任务的使用 spring通过任务执行器TaskExecutor来实现多线程与并发编程.通常使用ThreadPoolT ...

  7. spring dataSourceRouter自动切换数据源

    spring多数据源的切换,主要用到的是AbstractRoutingDataSource这个路由类,当我们的自定义的一个路由分发类继承AbstractRoutingDataSource类后,重写de ...

  8. Spring整合Quartz定时任务执行2次,Spring定时任务执行2次

    Spring整合Quartz定时任务执行2次,Spring定时任务执行2次 >>>>>>>>>>>>>>>&g ...

  9. Spring Boot自动配置源码解析(基于Spring Boot 2.0.2.RELEASE)

    在Spring Boot官方介绍中,首一段话是这样的(如下图).我们可以大概了解到其所表达的含义:我们可以利用Spring Boot写很少的配置来创建一个非常方便的基于Spring整合第三方类库的单体 ...

随机推荐

  1. Hadoop学习笔记之三 数据流向

    http://hadoop.apache.org/docs/r1.2.1/api/index.html 最基本的: 1. 文本文件的解析 2. 序列文件的解析 toString会将Byte数组中的内存 ...

  2. 淘宝客类别id大全

    汽车/用品/配件/改装 例 [ID:26] 家居饰品 例 [ID:50020808] 特色手工艺 例 [ID:50020857] 景点门票/度假线路/旅游服务 例 [ID:50025707] 男装 例 ...

  3. when i import skimage,it occurred --"cannot import name '_validate_lengths'"

    how to sovle this prolem? 1)with the administrator user to run cmd 2)imput and run : pip install --u ...

  4. [LuoguP2151][SDOI2009]HH去散步_递推_矩阵乘法_图论

    HH去散步 题目链接:https://www.luogu.org/problem/P2151 数据范围:略. 题解: 数据范围好小,让人不禁想用一些毒瘤算法,但是失败了. 这种类似时间啊这种有点重复味 ...

  5. SQL Server 学习之环境搭建

    SQL Server 环境搭建 说明:本文是sqlServer的安装和测试环境的搭建 版本是SQLServer 2005版,由于该版本只能在Windows7或者更低的系统上才能安装,更高的系统请安装S ...

  6. 【AtCoder】AGC011

    AGC011 A - Airport Bus 大意:有N个人,每个人只能在\([T_i,T_i +K]\)这段区间乘车,每辆车安排C人,问最少安排几辆车 直接扫,遇到一个没有车的在\(T_i +K\) ...

  7. linux安装java jdk

    一.检查是否已经安装java jdk. 检查是否有自带的openJdk, 若有将其卸载删除.详情第四步 二.jdk下载安装. 1.创建文件夹 mkdir /usr/local/java #自选目录,一 ...

  8. [转]Life of a binary

    发现一篇讲程序生命周期的文章,感觉蛮不错.

  9. 关于JS原型以及原型链、instanceof的一些理解

    一.JS原型 首先要区分两个概念 1.构造函数 2.实例:由构造函数通过new方式创建出来的就是实例 <script> function Foo() { } var f = new Foo ...

  10. fiddler笔记:TimeLine时间轴选项卡

    1.TimeLine选项卡介绍 TimeLine选项卡支持使用"瀑布"模型查看1~250个选中的Session.主要用于帮助性能分析和理解请求之间的关联.选项卡的主体内容是数据流视 ...