菜鳥初學 spring quartz 定時任務 希望對奮鬥在程序員的孩子們給予幫助

1、首先得需要在web.xml 文件中加載Sping_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" xmlns:jee="http://www.springframework.org/schema/jee"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

以上這是配置文件的開始

 

第二步 定義自己的定時任務類

 

 <bean id="dingshi" class="com.demo.lessiondingshi" scope="prototype">
  <property name="service">  // 由於是和spring結合的 注入了service層 這裏自己隨便定義了
   <ref bean="service" />
  </property>
 </bean>

 

第三部 定義任務類對象和調用的對象的方法

 

<bean id="dingshiclass"
  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <!--調用的類 -->
  <property name="targetObject" ref="dingshi" /> // 注意這裏ref 綁定自己的定時類
  <!--調用類中的方法 -->
  <property name="targetMethod" value="addCardstatics" /> 
 </bean>

 

第四步  定義觸發的時間

 

<bean id="dingshitime" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail">
   <ref bean="dingshiclass" />
  </property>
  <!--cron表達式 -->
  <property name="cronExpression">
   <!--循環時間設置-->
   <value>0 0/1 * ? * *</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 local="dingshitime"/>   </list>
  </property>
 </bean>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章