springmvc之定时器
一、通过注解方式实现定时器
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之定时器的更多相关文章
- springMVC实现定时器
//springmvc.xml文件配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...
- SpringMVC中定时器继承Task后无法对service注入问题
最近在做一个Spring+MyBatis的一个项目,其中用到了Redis的存储,然后遇到问题是这样的: RedisTask是通过定时器来每分钟像数据库里推送的,于是就有了 public class R ...
- Spring集成Memcached三种方式(一)
转载:http://blog.csdn.net/u013725455/article/details/52102170 Memcached Client目前有3种: Memcached Client ...
- spring核心及常用技术
一.核心内容 1.依赖注入(控制反转) 1)什么是依赖注入 spring将实例的创建交给spring容器(BeanFactory或ApplicationContext)管理,当实例创建后通过设值或构造 ...
- Memcached与Spring集成的方式(待实践)
主要是基于这几种方式http://www.cnblogs.com/EasonJim/p/7624822.html去实现与Spring集成,而个人建议使用Xmemcached去集成好一些,因为现在官方还 ...
- SpringMVC中使用Cron表达式的定时器
SpringMVC中使用Cron表达式的定时器 cron(定时策略)简要说明 顺序: 秒 分 时 日 月 星期 年份 (7个参数,空格隔开各个参数,年份非必须参数) 通配符: , 如果分钟位置为* 1 ...
- SpringMvc定时器任务
在最近的工作中,涉及到一个定时任务,由于以前对springMVC使用较少,所以,上网找了一点资料.这个demo感觉挺好,推荐给大家. 使用到的JAR文件: aopalliance-1.0.jarcom ...
- Springmvc 定时器的实现
有时候会需要项目中,定时去执行一些东西,这个时候就需要用到定时器了.比较简单, 当你springmvc环境搭建成功的时候. 本文转载自:https://www.cnblogs.com/wqj-blog ...
- springMVC + quartz实现定时器(任务调度器)
首先我们要知道任务调度器(定时器)有几种,这边我会写三种 第一种是基于JDK的本身的一个定时器(优点:简单,缺点:满足不了复杂的需求) package com.timer1; import java. ...
随机推荐
- 常见linux命令释义(第七天)——ulimit 与变量内容的删除替代与替换。
linux是一个多用户多任务的系统,不同于windows的单人多任务操作系统.再linux上,在同一个时间点上,可以有多个人同时执行多个任务. 那么假若有10个用户,同时打开了100个100M的文件. ...
- Nginx系列5之让Nginx支持HTTP1.1
preface nginx在反向代理HTTP协议的时候,默认使用的是HTTP1.0去向后端服务器获取响应的内容后在返回给客户端. HTTP1.0和HTTP1.1的一个不同之处就是,HTTP1.0不支持 ...
- CSS3-border-radius的兼容写法大全
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...
- wpf 窗体内容旋转效果 网摘
<Window x:Class="simplewpf.chuangtixuanzzhuan" xmlns="http://schemas.micros ...
- apache安装错误error: APR not found解决办法
linux安装时安装种类不同,一些组件包可能不会被安装,导致linux下安装软件的时候缺这个缺那个,今天为大家介绍linux安装apache时报apr找不到的解决办法 方法/步骤 下载依赖包 wg ...
- jpa datasource config
application.properties spring.datasource.driverClassName= spring.datasource.url= spring.datasource.u ...
- JSP 核心标签库
1 JSTL标签库介绍 JSTL标签库的使用是为弥补html标签的不足,规范自定义标签的使用而诞生的.使用JSLT标签的目的就是不希望在jsp页面中出现java逻辑代码 2 JSTL标签库分类 核心标 ...
- HTML5基本布局
HTML4布局 HTML5布局 基本的HTML5文档的模式为: <!DOCTYPE html> <html lang = "en"> <head> ...
- 学海无涯的整理Ing..........
1.文章:Understanding JavaScript Function Invocation and “this” http://yehudakatz.com/2011/08/11/unders ...
- C# Thread挂起线程和恢复线程
前言 众所周知,Thread类中的挂起线程和恢复线程微软已标记过时,因为可能会造成问题 Resume() 恢复当前线程 已过时. Resumes a thread that has been ...