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

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. 图解Python 【第六篇】:面向对象-类-进阶篇

    由于类的内容比较多,分为类-初级基础篇和类-进阶篇 本节内容一览图: 一.类成员修饰符 每一个类的成员都有两种形式: 公有成员,在任何地方都能访问 私有成员,只能在类的内部才能访问 1.1.私有成员和 ...

  2. switch语句 initialization of 'XXX' is skipped by 'case' label 原因及解决办法--块语句的作用

    出错代码段: switch (t) { case 0:  int a = 0;  break; default:  break; }编译时提示:“error C2361: initialization ...

  3. [转]SQL server 2008R2 中 C#Winfoirm 使用 SqlDependency 机制实现 数据库中某一张表的监视

    转自:https://blog.csdn.net/u012183487/article/details/77776930 System.Data.SqlClient命名空间下的 sqlDependen ...

  4. 安装flanal报错解决

    1.:Error registering network: failed to acquire lease: node "test4" pod cidr not assigned ...

  5. DISCUZ论坛各大功能模块入口文件介绍

    index.php 首页入口文件,这个文件相信大家都不陌生,小编就不具体介绍了. forum.php 论坛入口文件 portal.php 门户入口文件 group.php 群组入口文件 home.ph ...

  6. 虚拟IP技术 ip地址漂移技术

    虚拟IP地址(VIP) 是一个不与特定计算机或一个计算机中的网络接口卡(NIC)相连的IP地址.数据包被发送到这个VIP地址,但是所有的数据还是经过真实的网络接口.VIPs大部分用于连接冗余:一个VI ...

  7. java数据结构之自定义队列

    一.队列的特点 1.队列是线性结构 2.先进先出,先进入队列的排在队列前端,会比后进队列的先出队列.FIFO 二.通过数组来实现队列 //自己实现数组队列,队列的特定就是先进先出 public cla ...

  8. maven——将jar安装到本地仓库

    环境变量MAVEN_HOME配置正确后,cmd窗口执行此命令: mvn install:install-file -Dfile=C:\hehe.jar  -DgroupId=com.rockontro ...

  9. android#boardcast#发送自定义广播

    广播主要分为两种类型,标准广播和有序广播,通过实践的方式来看下这两种广播具体的区别. 一.发送标准广播 在发送广播之前,我们还是需要先定义一个广播接收器来准备接收此广播才行,不然发出去也是白发.因此新 ...

  10. APP安全_Android反编译

    反编译 Android的反编译工具:apktool,JEB等. Apk 文件的结构,如下: META-INF:签名文件 res:资源文件,里面的 xml 格式文件在编译过程中由文本格式转化为二进制的 ...