spring 的自动定时任务
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 的自动定时任务的更多相关文章
- Spring+Quartz 实现定时任务的配置方法
Spring+Quartz 实现定时任务的配置方法 整体介绍 一.Quartz介绍 在企业应用中,我们经常会碰到时间任务调度的需求,比如每天凌晨生成前天报表,每小时生成一次汇总数据等等.Quartz是 ...
- Spring整合Quartz定时任务 在集群、分布式系统中的应用(Mysql数据库环境)
Spring整合Quartz定时任务 在集群.分布式系统中的应用(Mysql数据库环境) 转载:http://www.cnblogs.com/jiafuwei/p/6145280.html 单个Q ...
- Eureka 系列(03)Spring Cloud 自动装配原理
Eureka 系列(03)Spring Cloud 自动装配原理 [TOC] 0. Spring Cloud 系列目录 - Eureka 篇 本文主要是分析 Spring Cloud 是如何整合 Eu ...
- spring类型自动转换——@InitBinder和Converter
spring有2种类型转换器,一种是propertyEditor,一种是Converter.虽然都是类型转换,但是还是有细微差别. 所以这里以一个例子的形式来分析一下这2种类型转换的使用场景和差别. ...
- 使用BeanNameAutoProxyCreator实现spring的自动代理
提到代理,我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置 ...
- spring多线程与定时任务
本篇主要描述一下spring的多线程的使用与定时任务的使用. 1.spring多线程任务的使用 spring通过任务执行器TaskExecutor来实现多线程与并发编程.通常使用ThreadPoolT ...
- spring dataSourceRouter自动切换数据源
spring多数据源的切换,主要用到的是AbstractRoutingDataSource这个路由类,当我们的自定义的一个路由分发类继承AbstractRoutingDataSource类后,重写de ...
- Spring整合Quartz定时任务执行2次,Spring定时任务执行2次
Spring整合Quartz定时任务执行2次,Spring定时任务执行2次 >>>>>>>>>>>>>>>&g ...
- Spring Boot自动配置源码解析(基于Spring Boot 2.0.2.RELEASE)
在Spring Boot官方介绍中,首一段话是这样的(如下图).我们可以大概了解到其所表达的含义:我们可以利用Spring Boot写很少的配置来创建一个非常方便的基于Spring整合第三方类库的单体 ...
随机推荐
- 【AMAD]django-filter -- 一个通用的,基于用户选择的Django Queryset 过滤系统
简介 用法 和DRF集成 个人评分 简介 django-filter1可以通过URL的query string参数,动态对Queryset进行过滤. 用法 import django_filters ...
- windows下exfat无法写入怎么修复?
为了能够实现mac与windows文件共享,把移动硬盘格式化为exfat了,可是在osx中放入文件后,在windows上紧进行读取写入时出现错误,提示使用chkdsk进行修正,下面是修正步骤. 方法/ ...
- ss user-rule自定义规则并硬连接到OneDrive进行自动同步
最近又换回Edge没了Switchy Omega用,仅仅使用GFWList感觉不够用,一些境内没服务器的网站直连还是挺慢的,于是就研究了一下PAC自定义规则. 简单说,平时用的规则就只有三种: 对于没 ...
- shell学习笔记3-后台执行命令
cron 系统调度进程,使用它在每天的某时间或一周.一月的不同时间运行 at 使用它在一个特点的时间允许一些特殊的作业 & 使用它在后台允许一个占用时间不长的进程 Nohup 使用它在后台运行 ...
- 冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
题1 素数 [问题描述] 给定一个正整数N,询问1到N中有多少个素数. [输入格式]primenum.in 一个正整数N. [输出格式]primenum.out 一个数Ans,表示1到N中有多少个素 ...
- textarea文本域轻松实现高度自适应
转载:http://www.xuanfengge.com/textarea-on-how-to-achieve-a-high-degree-of-adaptive.html 今天需要些一个回复评论的页 ...
- celery异步任务
1.安装 pip install celery 2.创建celery_tasks文件夹,创建config配置文件和main文件 # 在config.py文件中配置中间人broker地址信息 - red ...
- SQLite进阶-15.触发器
目录 触发器(Trigger) 触发器(Trigger)的要点: 触发器应用 查看触发器 删除触发器 触发器(Trigger) 触发器(Trigger)是数据库的回调函数,它会在指定的数据库事件发生时 ...
- Mac查看端口号是否被占用及释放
1.查看使用端口进程 lsof -i: 端口号 2.释放进程 kill 你的PID 3.再次执行第一步,是否无进程占用 lsof -i: 端口号
- Thinkphp命令行快速生成模型类方法
进去cmd,切换到项目根目录,也就是think文件所在目录,执行下面的指令可以生成index模块的blog模型类文件: >php think make:model index/Blog 生成的模 ...