spring定时器运用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jiangwudidebaba/article/details/82951073
前言: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);
		
		
	}
	
	

}

效果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/jiangwudidebaba/article/details/82951073