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

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上启动即可;

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

spring注解定时器的更多相关文章

  1. Spring注解定时器使用

    一.首先要配置我们的spring-service.xml 1.xmlns 多加下面的内容 xmlns:task="http://www.springframework.org/schema/ ...

  2. Spring注解实现定时功能以及Quartz定时器

    一:Spring注解实现--------->Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz 1:maven配置: <!-- quartz--> <d ...

  3. spring注解scheduled实现定时任务

    只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springfr ...

  4. Spring注解式与配置文件式

    http://tom-seed.iteye.com/blog/1584632 Spring注解方式bean容器管理 1.通过在配置文件中配置spring组件注入 <context:compone ...

  5. 你真的知道Spring注解驱动的前世今生吗?这篇文章让你豁然开朗!

    本篇文章,从Spring1.x到Spring 5.x的迭代中,站在现在的角度去思考Spring注解驱动的发展过程,这将有助于我们更好的理解Spring中的注解设计. Spring Framework ...

  6. spring注解源码分析--how does autowired works?

    1. 背景 注解可以减少代码的开发量,spring提供了丰富的注解功能.我们可能会被问到,spring的注解到底是什么触发的呢?今天以spring最常使用的一个注解autowired来跟踪代码,进行d ...

  7. Spring注解

    AccountController .java Java代码   1.        /** 2.         * 2010-1-23 3.         */ 4.        packag ...

  8. spring 注解的优点缺点

    注解与XML配置的区别 注解:是一种分散式的元数据,与源代码耦合. xml :是一种集中式的元数据,与源代码解耦. 因此注解和XML的选择上可以从两个角度来看:分散还是集中,源代码耦合/解耦. 注解的 ...

  9. spring注解说明之Spring2.5 注解介绍(3.0通用)

    spring注解说明之Spring2.5 注解介绍(3.0通用) 注册注解处理器 方式一:bean <bean class="org.springframework.beans.fac ...

随机推荐

  1. 查一张表占多少空间Bytes

    SELECT SUM(BYTES)/1024/1024||'MB' 占用空间 FROM dba_segments WHERE segment_name = '表名' AND owner = '用户名' ...

  2. shiro中接入单点登录功能

    最近新建的系统中使用了shiro,而shiro框架中包含登录认证和鉴权的功能,因为我们系统要统一接入公司内部的单点登录(isso)系统,所以通过isso的登录用户,需要在shiro中置为已认证,一下提 ...

  3. VBA MD5加密算法(转)

    ) ) Private Function LShift(lValue, iShiftBits) Then LShift = lValue Exit Function Then Then LShift ...

  4. RabbitMQ学习之:(十二)在Node.js环境下使用RabbitMQ

    学,以致用.找了半天Node.js下RabbitMQ的库,看上去都不太趁手,直到最后找到了amqp库,看上去倒还不错,照着例子,写了第一个RabbitMQ的客户端. 首先,使用 npm install ...

  5. MATLAB学习(四)线性方程求解,多项式运算,函数局部最优解

    >> A=[2 1 3;1 3 2];B=[5;10] B = 5 10 >> X=lsqnonneg(A,B) X = 0 2.8571 0.7143 >> A\ ...

  6. sql如何判断表字段是否存在默认值

    if exists(SELECT  1             FROM    sys.tables t                     INNER JOIN sys.columns c ON ...

  7. SpringBoot使用AOP获取请求参数

    最近试了下拦截controller方法查看请求参数,方式如下: package com.*.aop; import com.alibaba.fastjson.JSON; import com.alib ...

  8. 50道Kafka面试题和解析(转载)

    转载:https://zhuanlan.zhihu.com/p/78912551 前言 Apache Kafka的受欢迎程度很高,Kafka拥有充足的就业机会和职业前景.此外,在这个时代拥有kafka ...

  9. Go 采用 time.After 实现超时控制

    场景: 假设业务中需调用服务接口A,要求超时时间为5秒,那么如何优雅.简洁的实现呢? 我们可以采用select+time.After的方式,十分简单适用的实现. time.After()表示time. ...

  10. Python全栈开发之7、面向对象编程进阶-类属性和方法、异常处理和反射

    一.类的属性 1.@property属性 作用就是通过@property把一个方法变成一个静态属性 class Room: def __init__(self,name,length,width,he ...