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_ ...
随机推荐
- backreference Oracle正則表達式中的反向引用
这是Oracle对正則表達式的backreference的描写叙述 从定义中能够看到,当匹配表达式中已()的形式将一个子串包括起来.后面就能够以\? 的形式来引用.\1相应第一个(),\2相应第二 ...
- Spark Core(三)Executor上是如何launch task(转载)
1. 启动任务 在前面一篇博客中(Driver 启动.分配.调度Task)介绍了Driver是如何调动.启动任务的,Driver向Executor发送了LaunchTask的消息,Executor接收 ...
- hdu2159FATE(二维背包)
http://acm.hdu.edu.cn/showproblem.php?pid=2159 Problem Description 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在 ...
- PAT 1066 Root of AVL Tree[AVL树][难]
1066 Root of AVL Tree (25)(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, ...
- IDA 7.0在Mojava更新后打不开的问题
Mac升级到mojava后,ida 7.0打不开了. 上述是两种典型的窗口,不过不论是出现什么样的弹窗,如果是在升级之后出现的,都要试一下下面的解决办法.因为IDA7.0版本流出的比较多,虽然这个已经 ...
- 怎么在jquery里清空文本框的内容
$("input[name='test']").val("").focus(); // 将name=test的文本框清空并获得焦点,以便重新输入
- html07
1.复习js的外部对象,DOM,BOMBOM window -location -Location对象 : href reload() -history -History :back() forwar ...
- JMeter4.0二次开发之导入eclipse
1.先建立工程,命名为JMeter4.0. JDK版本为10.0.1 2.在官网下载src文件,通过文件系统导入到JMeter4.0工程中 3.在ant中选择download_jars,ant会自动下 ...
- linux常用命令:sort 命令
sort 命令是Linux系统下一种对文件排序的工具,sort 命令功能十分强大,是 Shell 脚本编程是常用的文件排序工具. 1.命令格式: sort [选项]... [文件]... 或:sort ...
- python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL
python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL实战例子:使用pyspider匹配输出带.html结尾的URL:@config(a ...