菜鸟初学 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>

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