導入jar包
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency>
實現需要執行的任務
public class CronJob { public void execute(){ System.out.println("I am CronJob"); } }
spring-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-4.1.xsd"> <!-- 加載job bean --> <bean id="myJob" class="com.inspur.SSM.quartz.CronJob" /> <!-- 將job bean 封裝成method --> <bean id="myMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="myJob" /> </property> <property name="targetMethod"> <value>execute</value> </property> </bean> <!-- 觸發器 --> <bean id="triggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="myMethod"></property> <property name="cronExpression" value="* * * * * ?"></property><!-- 秒 分 小時 日 月 周 年 --> </bean> <!-- 調度工廠 --> <bean id="schedulerBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false"> <property name="triggers"> <list> <ref bean="triggerBean" /> </list> </property> </bean> </beans>
spring+quartz實現定時任務
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.