您的位置:首页 > 编程语言 > Java开发

Spring定时器的使用

2015-12-24 10:28 459 查看
 

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

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

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

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

 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-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/tx   

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

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

          http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 <bean
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

  <!--
自动启动 -->

  <property
name="autoStartup">

   <value>true</value>

  </property>

  <property
name="triggers">

   <list>

    <ref
local="testTrigger" />

   </list>

  </property>

 </bean>

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

  <property
name="jobDetail">

   <ref
bean="testJobDetail" />

  </property>

  <property
name="cronExpression">

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

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

  </property>

 </bean>

 <bean id="testJobDetail"

  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

  <property
name="targetObject">

   <ref
bean="scheduleTimerTest" />

  </property>

  <property
name="targetMethod">

   <value>timeTest</value>

  </property>

  <property
name="concurrent" value="false" />

 </bean>

 <bean id="scheduleTimerTest"
class="com.lrq.timer.ScheduleTimerTest"></bean>

</beans>

这是配置文件,引入即可:

需要注意的地方:

1
jar包问题:ssh+\spring-context-support-3.2.1.RELEASE.jar+quartz-1.6.6.jar+quartz-all-1.6.6.jar

配置文件:

1 <bean id="scheduleTimerTest"
class="com.lrq.timer.ScheduleTimerTest"></bean>需要定时器执行的目标类;

需要被上面的定时器引用:

 <property
name="targetObject">

   <ref
bean="scheduleTimerTest" />

  </property>

  <property
name="targetMethod">

   <value>timeTest</value>

  </property>

2执行周期匹配表达式;请参看文档;

核心思路还是依赖注入;

 



转发至微博
 



转发至微博
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: