spring2.5.6下配置定时器

来源:互联网 发布:怎么通过网络找客户 编辑:程序博客网 时间:2024/05/21 11:17

applicationContext.xml 代码如下:

<?xml version="1.0"encoding="UTF-8"?> 

<beansxmlns="http://www.springframework.org/schema/beans" 

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop" 

    xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx" 

  xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd 

       http://www.springframework.org/schema/tx  

      http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 

      http://www.springframework.org/schema/aop  

      http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 

       http://www.springframework.org/schema/context  

      http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 

    <!-- 支持元注释 --> 

   <context:annotation-config /> 

     <!-- 扫描包目录 --> 

   <context:component-scanbase-package="com"></context:component-scan>      

  <importresource="scheduler.xml"/>       

</beans> 



scheduler.xml:代码如下:

<?xml version="1.0" encoding="GBK"?> 

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN""http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 

<beans> 

    <!-- 定时扫描周期,如果已到期,则结束周期 --> 

    <!-- 定时服务定义 -->    

   <beanclass="org.springframework.scheduling.quartz.SchedulerFactoryBean">    

        <!-- 自动启动 -->    

       <propertyname="autoStartup">    

         <value>true</value>    

        </property>    

       <propertyname="triggers">    

           <list>  

               <reflocal="testTrigger"/>   

           </list>    

        </property>    

    </bean>  

   <beanid="testTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean"> 

       <propertyname="jobDetail">    

          <refbean="testJobDetail"/>    

      </property>    

     <propertyname="cronExpression">    

           <!-- 过一秒开始,每间隔两秒执行-->    

            <value>1/2 ** * * ?</value>    

        </property>  

    </bean>  

    <beanid="testJobDetail"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 

        <propertyname="targetObject"> 

            <refbean="testJob"/> 

        </property>    

        <propertyname="targetMethod"> 

           <value>test</value> 

        </property>    

        <propertyname="concurrent" value="false"/>   

    </bean> 

    <beanid="testJob"class="com.jungle.TestJob"></bean> 

</beans> 

 


TestJob类:代码如下:

package com.jungle;   

public class TestJob { 

    public void test() { 

      System.out.println("test!!!");//运行效果是每间隔两秒打印这句话一次。 

   } 

 



 <dependency>
    <groupId>quartz-all</groupId>
    <artifactId>quartz-all</artifactId>
    <version>1.6.1</version>
 </dependency>


jar包下载: http://download.csdn.net/detail/shiyuezhong/4553072

原创粉丝点击