spring定時器運用
阿新 • • 發佈:2018-11-27
前言:spring 提供了相當強大的定時器功能使用也很簡單
接下來使用的是quartz2的程式碼(quartz1.x和quartz2.x配置不相同,主要使用的類不一樣)
配置程式碼:
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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 "> <bean id="quartzTest" class="com.mvc.quartz.QuartzTest"></bean> <bean id="jobDetailBean" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 執行的類 --> <property name="targetObject"> <ref bean="quartzTest" /> </property> <!-- 類中的方法 --> <property name="targetMethod"> <value>quartzShow</value> </property> </bean> <bean id="jobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="jobDetailBean"></property> <property name="cronExpression" value="0/5 * * * * ?" ></property> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="jobTrigger" /> </list> </property> </bean> </beans>
/** * <p>Title: QuartzTest.java</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2017</p> * <p>Company:kehui</p> * @author jiangcl * @date 2018年10月5日 * @version 1.0 */ package com.mvc.quartz; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; /** * <p>Title: QuartzTest</p> * <p>Description: </p> * @author jiangcl * @date 2018年10月5日 */ public class QuartzTest { public void quartzShow(){ System.out.println("定時器任務啟動了"); } }
啟動定時器:
/** * <p>Title: QuartzMain.java</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2017</p> * <p>Company:kehui</p> * @author jiangcl * @date 2018年10月5日 * @version 1.0 */ package com.mvc.quartz; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean; /** * <p>Title: QuartzMain</p> * <p>Description: </p> * @author jiangcl * @date 2018年10月5日 */ public class QuartzMain { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("/spring/spring-quartz.xml"); QuartzTest test = app.getBean(QuartzTest.class); } }
效果: