spring注解定时器
2024-09-05 09:34:37
上一篇文章写了一个在配置文件中设置时间的定时器,现在来写一个注解方式的定时器:
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上启动即可;
注解时间设置和上一篇文章一样;
最新文章
- BIOS MCSDK 2.0 学习笔记(一)
- 【Python④】python恼人的字符串,格式化输出
- Leetcode Partition List
- Android 自定义spinner下拉框实现的实现
- HDU-1042 N!
- Highchart :tooltip工具提示
- C语言学习_从VC++6.0开始
- ExtJS 4.2学习(一)——环境搭建
- Linux make nginx 的时候报错
- spring整合mybatis,批量扫描mapper接口出现异常
- CentOS openssh升级到openssh可用
- Mysql 计划任务
- android的体系结构
- Android 性能测试之CPU
- 致Play Framework开发者们的一封信
- [转] jquery $.ajax/$(document).ready is not a function的问题
- linux crontab 实现每秒执行的实例
- 每天一道剑指offer-二叉树的下一个结点1
- 分块+deque维护 Codeforces Round #260 (Div. 1) D. Serega and Fun
- hdu5297 Y sequence(容斥原理+迭代)