spring定时器的使用(spring2.5.6)

来源:互联网 发布:centos7 安装mysql 编辑:程序博客网 时间:2024/05/17 02:53

原文地址:http://blog.csdn.net/coooliang/article/details/6856351

之前有写过一篇spring3.0定时器的配置:http://blog.csdn.net/cl61917380/article/details/6265664

spring3.0确实是要简单好多,不过很多项目都还没有升级到spring3.0所以对于2.5的配置也需要了解。

我这里只说明spring的配置,如果不会整合的朋友可以查看我写的关于SSH或SSI整合的配置。

下面说明详细的配置:

applicationContext.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6.        http://www.springframework.org/schema/tx   
  7.        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
  8.        http://www.springframework.org/schema/aop   
  9.        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  10.        http://www.springframework.org/schema/context   
  11.        http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  12.     <!-- 支持元注释 -->  
  13.     <context:annotation-config />  
  14.   
  15.     <!-- 扫描包目录 -->  
  16.     <context:component-scan base-package="com"></context:component-scan>  
  17.       
  18.     <import resource="scheduler.xml"/>  
  19.       
  20. </beans>  


 

scheduler.xml:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="GBK"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">  
  3. <beans>  
  4.     <!-- 定时扫描周期,如果已到期,则结束周期 -->  
  5.     <!-- 定时服务定义 -->     
  6.     <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">     
  7.         <!-- 自动启动 -->     
  8.         <property name="autoStartup">     
  9.             <value>true</value>     
  10.         </property>     
  11.         <property name="triggers">     
  12.             <list>   
  13.                 <ref local="testTrigger"/>    
  14.             </list>     
  15.         </property>     
  16.     </bean>   
  17.     <bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
  18.         <property name="jobDetail">     
  19.             <ref bean="testJobDetail"/>     
  20.         </property>     
  21.         <property name="cronExpression">     
  22.             <!-- 过一秒开始,每间隔两秒执行-->     
  23.             <value>1/2 * * * * ?</value>     
  24.         </property>   
  25.     </bean>   
  26.     <bean id="testJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  27.         <property name="targetObject">  
  28.             <ref bean="testJob"/>  
  29.         </property>     
  30.         <property name="targetMethod">  
  31.             <value>test</value>  
  32.         </property>     
  33.         <property name="concurrent" value="false"/>    
  34.     </bean>  
  35.     <bean id="testJob" class="com.jungle.TestJob"></bean>  
  36. </beans>  

 

TestJob类:

[java] view plaincopyprint?
  1. package com.jungle;  
  2.   
  3. public class TestJob {  
  4.     public void test() {  
  5.         System.out.println("test!!!");//运行效果是每间隔两秒打印这句话一次。  
  6.     }  
  7. }  

 

所需要的jar包(我这里有引入struts的,一般SSH整合后的jar包就可以了):



0 0
原创粉丝点击