定时器功能我们一般不常用, 但是一旦用到,那也是非常重要的, 今天我们就讲一下如何简单快速的使用定时器

第一种方法, 使用注解的方式完成定时器

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"

2.然后使用task配置扫描注解

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

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

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

第二种方法, 不使用注解, 直接配置

  首先

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 加定时器的更多相关文章

  1. spring加载配置文件

    spring加载配置文件 1.把applicationContext.xml直接放在WEB-INF/classes下,spring会采用默认的加载方式2.采用在web.xml中配置ContextLoa ...

  2. Spring加载xsd引起的问题小记

    前言 最近要把之前写好的监控系统加上报警功能,就是通过rpc调用发短信发邮件的服务发送报警信息.发短信发邮件的功能是通过dubbo管理提供的.自然使用这些服务就难免用到spring.而我这又是一个st ...

  3. 监听spring加载完成后事件

    有这个想法是在很早以前了,那时的我没有接触什么缓存技术,只知道hibernate有个二级缓存.没有用过memcache,也没有使用过redis. 只懂得将数据放到数组里或者集合里,一直不去销毁它(只有 ...

  4. spring加载资源文件中classpath*与classpath的区别

    在spring和MyBatis继承的时候,配置mapperLocations.一开始配置是这样的. 需要加载路径为com/thomas/base/mapper和com/thomas/bu/mapper ...

  5. Spring任务调度定时器

    1.在spring-context.xml配置 <!-- 计划任务配置,用 @Service @Lazy(false)标注类,用@Scheduled(cron = "0 0 2 * * ...

  6. Spring加载流程源码分析03【refresh】

      前面两篇文章分析了super(this)和setConfigLocations(configLocations)的源代码,本文来分析下refresh的源码, Spring加载流程源码分析01[su ...

  7. Spring加载properties文件的两种方式

    在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...

  8. spring加载jar包中多个配置文件(转)

    转自:http://evan0625.iteye.com/blog/1598366 在使用spring加载jar包中的配置文件时,不支持通配符,需要一个一个引入,如下所示: Java代码 <co ...

  9. Dubbo实践(六)Spring加载Bean流程

    根据上一小节对于spring扩展schema的介绍,大概可以猜到dubbo中相关的内容是如何实现的. 再来回顾Dubbo实践(一)中定义的dubbo-provider.xml: <?xml ve ...

随机推荐

  1. Element-ui学习笔记3--Form表单(一)

    Radio单选框 要使用 Radio 组件,只需要设置v-model绑定变量,选中意味着变量的值为相应 Radio label属性的值,label可以是String.Number或Boolean. & ...

  2. cp拷贝

    1 cp 拷贝.复制 NAME cp - copy files and directories SYNOPSIS cp [OPTION]... [-T] SOURCE DEST        -- c ...

  3. 三分钟学会@Autowired@Qualifier@Primary注解

    三分钟学会@Autowired@Qualifier@Primary注解 2018.10.08 20:24 154浏览 今天主要简单的跟大家介绍一下spring自动装配相关的@Autowired,@Qu ...

  4. Python--day25--面向对象之封装

    狭义上的封装的例子:(例1)Python就只有两种类型:公有和私有,没有Java中说的那种保护类型 例2: 例3:正常的方法调用私有方法 封装总结:

  5. js 键盘事件keyCode 总结

    开发中经常页面中的某些按钮或元素需要绑定到键盘的输入事件 keydown.keyup 事件 keydown 键盘按下触发事件 $("#btn").keydown(function( ...

  6. Codeforces Round #564 (Div. 2) D. Nauuo and Circle(树形DP)

    D. Nauuo and Circle •参考资料 [1]:https://www.cnblogs.com/wyxdrqc/p/10990378.html •题意 给出你一个包含 n 个点的树,这 n ...

  7. H3C DHCP特点

  8. zoj 4124 "Median" (思维?假的图论?)

    传送门 来源:2019 年“浪潮杯”第十届山东省 ACM 省赛 题意: 对于一个包含n个数的(n为奇数)序列val[ ],排序后的 val[ (n+1) / 2 ] 定义为 median: 有 n 个 ...

  9. 使用iOSSelect.js实现iOS的select下拉选择日期的联动效果

    引入文件: <link rel="stylesheet" href="/static/css/iosSelect.css"> <script ...

  10. HttpServletRequest获得Url里面传来的值

    URL地址:http://XXXXX/manage/welcome?loginUser=123456String []str = request.getParameterValues("lo ...