上一篇文章写了一个在配置文件中设置时间的定时器,现在来写一个注解方式的定时器:

1.工程结构如下:

2.需要执行的代码块:

 package com.Task;

 import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class TaskDemo {
private static int sf = 0; @Scheduled(cron = "0/5 * * * * ?")
public void testTask(){
sf++;
System.out.println("5秒输出一次。。。。。sf:"+sf);
}
}
注意:不要忘记@Component,否则配置文件扫描不到需要执行的代码块;
3.
 <?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:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="com"/> <task:annotation-driven/> </beans>

说明:相对于上一遍的配置文件的设置时间方式,这种方式就简单多了,

如果启动过程中报:The prefix "task" for element "task:annotation-driven" is not bound.错误,

则需要在beans中加:

xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"

4.web.xml配置文件:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:conf/spring-task.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>

web.xml配置的信息目的就是让spring-task.xml文件生效;

5.相关jar包:http://pan.baidu.com/s/1qYwCrBQ

6.部署到tomcat上启动即可;

注解时间设置和上一篇文章一样;

最新文章

  1. BIOS MCSDK 2.0 学习笔记(一)
  2. 【Python④】python恼人的字符串,格式化输出
  3. Leetcode Partition List
  4. Android 自定义spinner下拉框实现的实现
  5. HDU-1042 N!
  6. Highchart :tooltip工具提示
  7. C语言学习_从VC++6.0开始
  8. ExtJS 4.2学习(一)——环境搭建
  9. Linux make nginx 的时候报错
  10. spring整合mybatis,批量扫描mapper接口出现异常
  11. CentOS openssh升级到openssh可用
  12. Mysql 计划任务
  13. android的体系结构
  14. Android 性能测试之CPU
  15. 致Play Framework开发者们的一封信
  16. [转] jquery $.ajax/$(document).ready is not a function的问题
  17. linux crontab 实现每秒执行的实例
  18. 每天一道剑指offer-二叉树的下一个结点1
  19. 分块+deque维护 Codeforces Round #260 (Div. 1) D. Serega and Fun
  20. hdu5297 Y sequence(容斥原理+迭代)

热门文章

  1. [Java]简单计算下一段Java代码段运行了多少秒
  2. Rsync数据同步工具及sersync同步工具
  3. Repeater, DataList, 和GridView的区别及使用
  4. ART模式
  5. 史上最全最详细JNDI数据源配置说明
  6. Fluent操作流程&amp;&amp;udf编译
  7. 前端面试经典题之apply与call的比较
  8. 从零探索Java网络编程01之 TCP/IP 与 Socket
  9. 奥比中光Astra Pro的使用(1)
  10. 50道Kafka面试题和解析(转载)