首页 > 编程语言 > 详细

spring定时器的配置

时间:2017-04-28 17:44:53      阅读:299      评论:0      收藏:0      [点我收藏+]

首先,新建一个java项目,下面导入需要的jar包:

 

技术分享

这里有你需要的jar包哦。

jar包下载

在src文件夹下,新建一个applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
  
      <!-- Spring 定时器演示 -->

<!--     定时器开关   -->
    <task:annotation-driven /> 
  
    <bean id="LogTask" class="com.test.Test"></bean>  
  
    <task:scheduled-tasks>  
<!--         这里表示的是2s执行一次    -->
        <task:scheduled ref="LogTask" method="execute" cron="*/2 * * * * ?" />  
    </task:scheduled-tasks>  
      
    
</beans>
     

在web.xml中配置:

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

创建定时启动的java类:

test.java

package com.test;

public class Test {
    public void execute() {
        System.out.println("定时器启动了。。。");
    }
}

部署在Tomcat中启动Tomcat即可。

具体定时器执行的时间可以再行研究。

此demo亲测好使。。。。。

spring定时器的配置

原文:http://www.cnblogs.com/zjiacun/p/6782495.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!