Spring 定时器容易配置

Spring 定时器简单配置

要使用Spring的定时器,首先必须得所定时器的支持jar包加载到项目中的lib下。(quartz-1.7.3.jar---附件中可以下载)

在web.xml中加载spring的这个配置文件我就不多讲了。我喜欢把这个配置文件命名为(applicationContext-quartz.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <!-- 要调用的工作类 -->
    <bean id="TaskTest" class="com.freshpower.pgs.sms.task.Task"></bean>
    <!-- 可继续加新的任务   -->
    <!-- 要调用的工作类结束 -->
   
    <!-- 定义调用对象和调用对象的方法 -->
    <bean id="jobtaskTest" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 调用的类 -->
        <property name="targetObject">
            <ref bean="TaskTest"/>
        </property>
        <property name="targetMethod">
            <value>Load</value>
        </property> 
    </bean>
   <!-- 可继续加新的   -->
   <!-- 定义调用对象和调用对象的方法结束 -->
  
   <!-- 定义触发时间 -->
   <bean id="doTimeTest" class="org.springframework.scheduling.quartz.CronTriggerBean">
           <property name="jobDetail">
            <ref bean="jobtaskTest"/>
        </property>
        <!-- cron表达式 此处定义为一直触发执行任务 -->
        <property name="cronExpression">
            <!--<value>10,15,20,25,30,35,40,45,50,55 * * * * ?</value> -->
            <value>1/5 * * * * ?</value>
        </property>
   </bean>
   <!-- 可继续加新的   -->
   <!-- 定义触发时间结束 -->

    <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序   -->
    <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
              <ref bean="doTimeTest"/>
              <!-- 可继续加新的   -->
            </list>
         </property>
    </bean>
    <!-- 总管理类结束   -->
</beans>

 

cron表达式在前一编文章中已经贴过了。有兴趣的可以去看看。