本文为博主原创,未经允许不得转载

项目中要经常事项定时功能,在网上学习了下用spring的定时功能,基本有两种方式,在这里进行简单的总结,

以供后续参考,此篇只做简单的应用。

1.在spring-servlet.xml文件中加入task的命名空间:

  1. xmlns:task="http://www.springframework.org/schema/task"
  2. xsi:schemaLocation="http://www.springframework.org/schema/task
  3. http://www.springframework.org/schema/task/spring-task-4.0.xsd"

然后使用task配置扫描注解

  1. <!-- 定时任务 -->
  2. <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
  3. <task:scheduler id="qbScheduler" />

此时就可以直接使用@Scheduled(cron = "时间格式串"),应用该注解就可以实现定时的功能

  1. @Scheduled(cron = "0/5 * * * * ?") //每隔5秒执行一次定时任务
  2. public void consoleInfo(){
  3. System.out.println("定时任务");
  4. }

第二种方法为:不使用注解实现定时任务,将定时的功能在spring配置文件中实现。

  1. 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”
  2.  
  3. <description>
  4. 定时任务
  5. </description>
    //定时注解驱动
  6. <task:annotation-driven />
  7. //进行定时任务的类,将其定义为一个bean
  8. <bean id="spaceStatisticsService" class="com.pojo.system.manager.sigar.impl.SpaceStatisticsServiceImpl"></bean>
    //通过task标签,定义定时功能
  9. <task:scheduled-tasks>
  10. <task:scheduled ref="spaceStatisticsService" method="statisticSpace" cron="59 59 23 * * ?" />
  11. </task:scheduled-tasks>

要实现的代码部分为:

  1. @Service
  2. public class SpaceStatisticsServiceImpl implements SpaceStatisticsService
  3. {
  4. @Override
  5. public void statisticSpace()
  6. {
  7. System.out.println("实现定时功能");
  8. }
  9. }

总结:两种方法都能实现定时的功能,但明显第一种方式会比较简洁,而且更加方便。

spring实现定时任务的两种方式的更多相关文章

  1. spring实现定时任务的两种方式之spring @scheduled注解方式

    1.使用spring的 scheduled使用注解的方式 这种方法的好处是:使用方便,配置少,提高开发效率: 缺点是:如果使用服务器集群部署方式的时候,其自身无法解决定时任务重复执行的问题. 2.首先 ...

  2. Spring创建JobDetail的两种方式

    一.Spring创建JobDetail的两种方式 二.整合方式一示例步骤 1.将spring核心jar包.quartz.jar和Spring-context-support.jar导入类路径. 2.编 ...

  3. spring配置属性的两种方式

    spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...

  4. Spring Boot 中实现定时任务的两种方式

    在 Spring + SpringMVC 环境中,一般来说,要实现定时任务,我们有两中方案,一种是使用 Spring 自带的定时任务处理器 @Scheduled 注解,另一种就是使用第三方框架 Qua ...

  5. Spring整合Struts的两种方式介绍

    1 使用Spring托管Struts Action 该种方式就是将Struts Action也视为一种Bean交给Spring来进行托管,使用时Struts的配置文件中配置的Action的classs ...

  6. Spring整合Hibernate的两种方式

    在使用spring注解整合hibernate时出现"org.hibernate.MappingException: Unknown entity: com.ssh.entry.Product ...

  7. spring 注入bean的两种方式

    我们都知道,使用spring框架时,不用再使用new来实例化对象了,直接可以通过spring容器来注入即可. 而注入bean有两种方式: 一种是通过XML来配置的,分别有属性注入.构造函数注入和工厂方 ...

  8. Spring定义Bean的两种方式:和@Bean

    前言:    Spring中最重要的概念IOC和AOP,实际围绕的就是Bean的生成与使用. 什么叫做Bean呢?我们可以理解成对象,每一个你想交给Spring去托管的对象都可以称之为Bean. 今天 ...

  9. ubuntu下创建定时任务的两种方式及常见问题解决方案

    创建定时任务的目的就是摆脱人为对程序重复性地运行. 0. 首先用下面的指令检查你是否安装crontab, crontab -l 如果本身就有的话,那么出现如下指令 LC_CTYPE="zh_ ...

随机推荐

  1. 【小甲鱼】【Python】正则表达式(二)

    >>> import re#|表示或的意思 >>> re.search(r"Fish(C|D)","FishC") < ...

  2. 003-spring cloud gateway-概述、Route模型、网关初始化配置过程、基本原理

    一.概述 网关服务核心是将进入的请求正确合理的路由到下层具体的服务进行业务处理,由此可见网关服务的核心就是路由信息的构建. Spring Cloud Gateway旨在提供一种简单而有效的方式来路由到 ...

  3. 使用gradle构建多模块springboot项目,打jar包

    官方文档: https://spring.io/guides/gs/rest-service/  参考:http://blog.csdn.net/u013360850/article/details/ ...

  4. Integer.valueOf方法的源码解读

    public class IntegerDemo { public static void main(String[] args) { Integer i01 = ; ; Integer i03 = ...

  5. soapUI-property Transfer

    1.1.1  Property Transfer 创建或双击现有的Property-Transfer TestStep将打开以下窗口: 左侧的列表显示了此TestStep中配置的传输,添加和管理所需的 ...

  6. Summary: Java Inheritance

    In this tutorial we will discuss about the inheritance in Java. The most fundamental element of Java ...

  7. css3 弹性盒模型 变化

    如果你使用google搜索Flexbox,你会发现很多过时的信息.这里将告诉你如何迅速的辨别你需要的信息. 如果你正在查找有关于Flexbox的博客资料,你看到了“display:box;”或者“bo ...

  8. linux常用命令:Linux 文件类型与扩展名

    Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序而创建的比如file.txt.file.tar.gz ,这些文件虽然要用不同的程序来打开,但放在Linux文 ...

  9. python进程join()函数理解

    Join()是主程序等我这个进程执行完毕了,程序才往下走  

  10. Filter过滤器原理和登录实现

    Filter过滤器API      Servlet过滤器API包含了3个接口,它们都在javax.servlet包中,分别是Filter接口.FilterChain接口和FilterConfig接口. ...