spring实现定时任务的两种方式
本文为博主原创,未经允许不得转载
项目中要经常事项定时功能,在网上学习了下用spring的定时功能,基本有两种方式,在这里进行简单的总结,
以供后续参考,此篇只做简单的应用。
1.在spring-servlet.xml文件中加入task的命名空间:
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd"
然后使用task配置扫描注解
<!-- 定时任务 -->
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" />
此时就可以直接使用@Scheduled(cron = "时间格式串"),应用该注解就可以实现定时的功能
@Scheduled(cron = "0/5 * * * * ?") //每隔5秒执行一次定时任务
public void consoleInfo(){
System.out.println("定时任务");
}
第二种方法为:不使用注解实现定时任务,将定时的功能在spring配置文件中实现。
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation=" http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd” <description>
定时任务
</description>
//定时注解驱动
<task:annotation-driven />
//进行定时任务的类,将其定义为一个bean
<bean id="spaceStatisticsService" class="com.pojo.system.manager.sigar.impl.SpaceStatisticsServiceImpl"></bean>
//通过task标签,定义定时功能
<task:scheduled-tasks>
<task:scheduled ref="spaceStatisticsService" method="statisticSpace" cron="59 59 23 * * ?" />
</task:scheduled-tasks>
要实现的代码部分为:
@Service
public class SpaceStatisticsServiceImpl implements SpaceStatisticsService
{
@Override
public void statisticSpace()
{
System.out.println("实现定时功能");
}
}
总结:两种方法都能实现定时的功能,但明显第一种方式会比较简洁,而且更加方便。
spring实现定时任务的两种方式的更多相关文章
- spring实现定时任务的两种方式之spring @scheduled注解方式
1.使用spring的 scheduled使用注解的方式 这种方法的好处是:使用方便,配置少,提高开发效率: 缺点是:如果使用服务器集群部署方式的时候,其自身无法解决定时任务重复执行的问题. 2.首先 ...
- Spring创建JobDetail的两种方式
一.Spring创建JobDetail的两种方式 二.整合方式一示例步骤 1.将spring核心jar包.quartz.jar和Spring-context-support.jar导入类路径. 2.编 ...
- spring配置属性的两种方式
spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...
- Spring Boot 中实现定时任务的两种方式
在 Spring + SpringMVC 环境中,一般来说,要实现定时任务,我们有两中方案,一种是使用 Spring 自带的定时任务处理器 @Scheduled 注解,另一种就是使用第三方框架 Qua ...
- Spring整合Struts的两种方式介绍
1 使用Spring托管Struts Action 该种方式就是将Struts Action也视为一种Bean交给Spring来进行托管,使用时Struts的配置文件中配置的Action的classs ...
- Spring整合Hibernate的两种方式
在使用spring注解整合hibernate时出现"org.hibernate.MappingException: Unknown entity: com.ssh.entry.Product ...
- spring 注入bean的两种方式
我们都知道,使用spring框架时,不用再使用new来实例化对象了,直接可以通过spring容器来注入即可. 而注入bean有两种方式: 一种是通过XML来配置的,分别有属性注入.构造函数注入和工厂方 ...
- Spring定义Bean的两种方式:和@Bean
前言: Spring中最重要的概念IOC和AOP,实际围绕的就是Bean的生成与使用. 什么叫做Bean呢?我们可以理解成对象,每一个你想交给Spring去托管的对象都可以称之为Bean. 今天 ...
- ubuntu下创建定时任务的两种方式及常见问题解决方案
创建定时任务的目的就是摆脱人为对程序重复性地运行. 0. 首先用下面的指令检查你是否安装crontab, crontab -l 如果本身就有的话,那么出现如下指令 LC_CTYPE="zh_ ...
随机推荐
- [Google Maps API 3]Marker从Clusterer中分离及Marker置于Cluster上一层的解决办法
在Google Maps API的使用中,经常用到Clusterer来避免过密的Marker显示.但仔细看一下Clusterer的设置参数中并没有直接将某些Marker除外的方法,那遇到这样的需求,怎 ...
- SQL Server中SCAN 和SEEK的区别
SQL Server中SCAN 和SEEK的区别 SQL SERVER使用扫描(scan)和查找(seek)这两种算法从数据表和索引中读取数据.这两种算法构成了查询的基础,几乎无处不在.Scan会扫描 ...
- 全套 AR 应用设计攻略都在这里!
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/jILRvRTrc/article/details/79823908 通过将虚拟内容与现实世界融合,增 ...
- 一行代码彻底禁用WordPress缩略图自动裁剪功能
记得在博客分享七牛缩略图教程的时候,提到过 WordPress 默认会将上传的图片裁剪成多个,不但占用磁盘空间,也会拖慢网站性能,相当闹心! 当时也提到了解决办法: ①.关闭主题自带缩略图裁剪功能(若 ...
- [py]django前台处理后端返回的各类数据
参考 要完成的任务 返回str 返回list 返回arr 前端遍历 关键字 if for语句处理str list dict - 遍历字典 for语句 {% for key, value in info ...
- How many Fibs?(poj 2413)大数斐波那契
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=259#problem/C Description Recall the defi ...
- [LeetCode] 101. Symmetric Tree_ Easy tag: BFS
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- ftp命令行敲不了
最先安装了vsftpd,但是命令行敲ftp老是不行 解决方案:ftp命令是ftp客户端,vsftp是ftp服务器,两者不是一个概念.你需要安装ftp客户端 yum install ftp 可以自动安装 ...
- UVALive - 7261 Xiongnu's Land
思路: 先二分下界,再二分上届. #include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB ...
- zoj3814
这题说的是给了一个数在longlong范围内 然后求出小于这个数的最大的回文,枚举每位减去1后 , 他后面的位置上全部都置为9,然后在枚举每个前半部分,然后贪心取得这个数的最大值,贪心的时候写错了,错 ...