一、通过注解方式实现定时器

1、工程结构

2、所需jar包

3、spring-config.xml,springmvc配置文件

<?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:oxm="http://www.springframework.org/schema/oxm"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <!-- 通知spring容器通过注解的方式装配bean -->
<context:annotation-config />
<!-- 通知spring容器采用自动扫描机制查找注解的bean -->
<context:component-scan base-package="com.*" /> <task:annotation-driven /> <!-- 定时器开关--> <!--  配置返回页面过滤 --> 
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

注:下面是需要引入头路径:

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.2.xsd"

下面是定时器开关配置:

<task:annotation-driven/>

4、web.xml,加载springmvc配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>spring</display-name> <!-- springmvc配置 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

5、TimerController.java

package com.timer;

import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class TimerController { /**
* 每隔20秒执行一次
*/
@Scheduled(fixedRate = 1000*20)
public void print(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");
System.out.println("timer : "+format.format(new Date()));
} }

注:使用注解类前必须有@Component

二、通过配置xml文件实现定时器

1、spring-config.xml,springmvc配置

<?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:oxm="http://www.springframework.org/schema/oxm"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <!-- 通知spring容器通过注解的方式装配bean -->
<context:annotation-config />
<!-- 通知spring容器采用自动扫描机制查找注解的bean -->
<context:component-scan base-package="com.*" /> <task:annotation-driven /> <!-- 定时器开关--> <bean id="agentExcelTask" class="com.timer.TimerController1"/>
<task:scheduled-tasks>
<task:scheduled ref="agentExcelTask" method="print" cron="0/5 * * * * ?"/>
</task:scheduled-tasks> <!-- 配置返回页面过滤 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

注: 多了创建bean和时间配置,每隔5秒触发一次,代码如下:

<bean id="agentExcelTask" class="com.timer.TimerController1"/>
<task:scheduled-tasks>
<task:scheduled ref="agentExcelTask" method="print" cron="0/5 * * * * ?"/>
</task:scheduled-tasks>

注:method="print"指明触发的为TimerController1类中的print方法。

2、TimerController1.java

package com.timer;

import java.text.SimpleDateFormat;
import java.util.Date; public class TimerController1 { public void print(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");
System.out.println("timer1 : "+format.format(new Date()));
} }

注:时间格式参考

springmvc之定时器的更多相关文章

  1. springMVC实现定时器

    //springmvc.xml文件配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...

  2. SpringMVC中定时器继承Task后无法对service注入问题

    最近在做一个Spring+MyBatis的一个项目,其中用到了Redis的存储,然后遇到问题是这样的: RedisTask是通过定时器来每分钟像数据库里推送的,于是就有了 public class R ...

  3. Spring集成Memcached三种方式(一)

    转载:http://blog.csdn.net/u013725455/article/details/52102170 Memcached Client目前有3种: Memcached Client ...

  4. spring核心及常用技术

    一.核心内容 1.依赖注入(控制反转) 1)什么是依赖注入 spring将实例的创建交给spring容器(BeanFactory或ApplicationContext)管理,当实例创建后通过设值或构造 ...

  5. Memcached与Spring集成的方式(待实践)

    主要是基于这几种方式http://www.cnblogs.com/EasonJim/p/7624822.html去实现与Spring集成,而个人建议使用Xmemcached去集成好一些,因为现在官方还 ...

  6. SpringMVC中使用Cron表达式的定时器

    SpringMVC中使用Cron表达式的定时器 cron(定时策略)简要说明 顺序: 秒 分 时 日 月 星期 年份 (7个参数,空格隔开各个参数,年份非必须参数) 通配符: , 如果分钟位置为* 1 ...

  7. SpringMvc定时器任务

    在最近的工作中,涉及到一个定时任务,由于以前对springMVC使用较少,所以,上网找了一点资料.这个demo感觉挺好,推荐给大家. 使用到的JAR文件: aopalliance-1.0.jarcom ...

  8. Springmvc 定时器的实现

    有时候会需要项目中,定时去执行一些东西,这个时候就需要用到定时器了.比较简单, 当你springmvc环境搭建成功的时候. 本文转载自:https://www.cnblogs.com/wqj-blog ...

  9. springMVC + quartz实现定时器(任务调度器)

    首先我们要知道任务调度器(定时器)有几种,这边我会写三种 第一种是基于JDK的本身的一个定时器(优点:简单,缺点:满足不了复杂的需求) package com.timer1; import java. ...

随机推荐

  1. 捉襟见肘之message sent to deallocated instance 0x16f62a70

    出现的问题(真机ios8到ios9测试没有问题,真机ios7.1出现问题): -- :::60b] *** -[ChatViewController scrollViewDidScroll:]: me ...

  2. Linux学习之CentOS--CentOS6.下Mysql数据库的安装与配置

    跟着配置,顺利配置完成 http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html

  3. ionic的scroll的使用出现的问题

    今天再写ionic的时候发现了一个问题,在使用ui-view的时候引用ion-scroll指令的时候初次加载的时候能能实现拖拽滚动,只能实现滚动鼠标滚轴滚动,然后各种调试,各种比较,终于发现原来是路由 ...

  4. 关于Thread.Sleep(0)

    看到的文章,写的不错. 我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢?思考下面这两个问题:假设现在是 2008-4-7 12:00:0 ...

  5. base64 json

    javascript将base64编码的图片数据转换为file并提交 直接提交base64编码图片数据,过大的话后台会出现转发错误问题. 一个不错的解决方式就是将base64编码的图片数据转换为Blo ...

  6. js日期加减

    先补充下基础知识: var myDate = new Date(); //myDate默认返回当前时间 myDate.getYear(); //获取当前年份(2位) myDate.getFullYea ...

  7. 9月12日JavaScript脚本语言

    JS脚本语言 JS脚本语言全称JavaScript,是网页里面使用的脚本语言,也是一门非常强大的语言. 一.基础语法 1.注释语法 单行注释:// 多行注释:/**/ 2.输出语法 ①alert(信息 ...

  8. JavaWeb文件下载,提示用户保存而不是让浏览器直接打开

    1.通过HttpServletResponse对象实现文件下载 服务端向客户端游览器发送文件时,如果是浏览器支持的文件类型,一般会默认使用浏览器打开,比如txt.jpg等,会直接在浏览器中显示,如果需 ...

  9. dedecms笔记

    截取字符串 方法一: [field:title function="cn_substr(@me,10)"/] 方法二: {dede:arclist typeid=’9′ title ...

  10. Exception Handling in ASP.NET Web API webapi异常处理

    原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes erro ...