利用Spring集成Quartz能够轻松实现一个定时器,其中Quartz的配置applicationContext-quartz.xml以下:java
<?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-3.2.xsd" default-lazy-init="true"> <description>Quartz的本地Cron式执行任务配置</description> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="autoStartup"><value>true</value></property> <property name="triggers"> <list> <ref local="scheduleFactoryTrigger"/> </list> </property> </bean> <bean id="scheduleFactoryTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"><ref bean="scheduleFactory" /></property> <property name="cronExpression"> <value>0 35 10 * * ?</value> </property> </bean> <bean id="schedule" class="要执行的方法所在类的全路径"></bean> <bean id="scheduleFactory" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"><ref bean="schedule" /></property> <property name="targetMethod"><value>execute</value></property><!--执行execute方法--> <property name="concurrent" value="false" /> </bean> </beans>
其中cronExpression能够根据本身的须要配置执行方法的时间,具体配置能够参考cronExpression语法详解。web
而后须要在配置文件web.xml中添加对Quartz的引用,保证当服务器启动的时候,计时器就会生效。添加以下:spring
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:/applicationContext-quartz.xml, </param-value> </context-param>