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()));
    } 

}

注:时间格式参考

时间: 2024-08-08 05:36:25

springmvc之定时器的相关文章

springMVC设置定时器定时执行某个方法

   <!--人事档案工龄 在applicationContext.xml中配置-->   <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->         <bean id="pstartQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.

Spring集成Memcached三种方式(一)

转载:http://blog.csdn.net/u013725455/article/details/52102170 Memcached Client目前有3种: Memcached Client for Java SpyMemcached XMemcached 这三种Client一直存在各种争议: Memcached Client for Java 比 SpyMemcached更稳定.更早.更广泛: SpyMemcached 比 Memcached Client for Java更高效: X

SpringMVC使用Cron表达式的定时器

SpringMVC的功能很强大,集成了Quartz定时器的功能.能够通过Cron表达式和简单的注解就实现定时运行任务的功能. 网上看到不少样例,可是都不是非常全. 闲话少说.首先要在springmvc.xml中加入以下几行: xmlns:task="http://www.springframework.org/schema/task" <!--以下两行要放在xsi:schemaLocation里面--> http://www.springframework.org/sche

SpringMvc定时器任务

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

springMVC框架配置定时器

在springmvc.xml添加如下内容在xmlns中添加 xmlns:task="http://www.springframework.org/schema/task"1在xsi中添加 http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.2.xsd12在中间添加 <!-- 设置定时任务 --><task:annotatio

Springmvc定时器

applicationContext-jobs.xml 定时器配置<?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:task=&qu

使用SpringMVC写一个定时器

一.首先xml头部增加几个标签 xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd" 二.开启扫描注解.(注意这个是一定要开启的.否则无效,有时还要注意如果只扫描Controller时的处理) <con

springmvc 定时器

CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明  是否必填  允许填写的值 允许的通配符 1  秒  是  0-59    , - * / 2  分  是  0-59   , - * / 3 小时  是  0-23   , - * / 4  日  是  1-31   , - * ? / L W 5  月  是  1-12 or JAN-DEC   , - * / 6  周  是  1-7 or SUN-SAT   , - * ? /

SpringMVC自带的定时器,超好用,基于注解

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd 配置文件中加入这个 <!-- task任务扫描注解 --> <task:annotation-driven/> <context:component-scan base-package="com.*******.tog.*.time"><