spring定时器配备(quartz)

spring定时器配置(quartz)
一、定时器类简单代码:功能每5分钟打印一次
package meiyx.com;

import java.util.Date;

import org.springframework.stereotype.Component;

@Component
public class PrintTimer {
	public void print(){
		System.out.println(new Date(System.currentTimeMillis())+"------->meiyx");
	}
}


二、spring的相关配置文件:
config.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"

	xsi:schemaLocation="http://www.springframework.org/schema/beans    
           http://www.springframework.org/schema/beans/spring-beans-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/tx   
      	   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
       	   http://www.springframework.org/schema/aop   
      	   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	<!-- 请放置在文件末尾 -->
	<context:component-scan base-package="meiyx.com">
		
	</context:component-scan>
</beans>

三、定时器相关配置 timer.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"

	xsi:schemaLocation="http://www.springframework.org/schema/beans    
           http://www.springframework.org/schema/beans/spring-beans-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/tx   
      	   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
       	   http://www.springframework.org/schema/aop   
      	   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	
	<!--
		cronExpression的介绍: 
		按顺序 <value> 秒 分 小时 日期 月份 星期 年<value> 
		字段 		允许值 				允许的特殊字符
		秒 			0-59 				, - * / 
		分 			0-59 				, - * / 
		小时			0-23 				, - * / 
		日期 		1-31 				, - * ? / L W C
		月份 		1-12 或者 JAN-DEC 	, - * / 
		星期			1-7 或者 SUN-SAT 		, - * ? / L C # 
		年		(可选)留空,1970-2099 	, - * / 
		“*”字符被用来指定所有的值。如:”*“在分钟的字段域里表示“每分钟”。
		参考《http://www.cnblogs.com/xiaopeng84/archive/2009/11/26/1611427.html》
	-->
	<!-- 定时单期发行数据处理开始,每天02:00执行一次:0 0 2 * * ?-->
	<bean id="printTimerJob"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="printTimer" />
		<property name="targetMethod" value="print" />
	</bean>
	<bean id="printTimerTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="printTimerJob" />
		<property name="cronExpression" value="0 0/5 * * * ?" />
	</bean>
	
	<!-- 启动定时器 -->
	<bean id="schedulerFactoryBean"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="applicationContextSchedulerContextKey" value="applicationContext"/>
		<property name="triggers">
			
			<list>
				<ref bean="printTimerTrigger" />
			</list>
		</property>
	</bean>
	<!-- end -->
</beans>


四、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!--  Spring 服务层的配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
        	classpath*:config.xml
        	classpath*:timer.xml
        </param-value>
	</context-param>
	<!--  Spring 容器启动监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  
</web-app>


以上是实现定时器的简单过程,测试部署到tomcat或jboss即可;
有疑问联系Email:meiyx89@163.com,qq:532030490