首先是配置spring监听到web.xml中
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/applicationContext-bl.xml</param-value>
</context-param>
然后整合spring和quart
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 启动触发器的配置开始 -->
<bean name="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myJobTrigger" /><!--当然一个scheduler可以管理多个trigger,只需在list中配置即可-->
</list>
</property>
</bean>
<!-- 启动触发器的配置结束 -->
<!-- 调度的配置开始 -->
<!--
quartz-1.8以前的配置
<bean id="myJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="myJobDetail" />
</property>
<property name="cronExpression">
<value>0/1 * * * * ?</value>
</property>
</bean>
-->
<!-- quartz-2.x的配置 -->
<bean id="myJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="myJobDetail" />
</property>
<property name="cronExpression">
<value>60 * * * * ?</value>
</property>
</bean>
<!-- 调度的配置结束 -->
<!-- job的配置开始 -->
<bean id="myJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="myJob" />
</property>
<property name="targetMethod">
<value>work</value>
</property>
</bean>
<!-- job的配置结束 -->
<!-- 工作的bean -->
<bean id="myJob" class="com.ssh.common.quartz.MyJob" />
</beans>
MyJob就是自定义的定时任务。
关于定时任务执行的时间,网上有很多,这块真的很灵活。