1.基础jar包:
2.建applicationContext-quartz.xml的配置 :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" 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-2.5.xsd">
<!-- 定义了一个任务 -->
<bean id="quartzClock" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.sgfm.datacenter.sys.DataTimingTask</value><!-- 类的名字 有这个类完成具体的 任务 -->
</property>
</bean>
<!-- 定义了任务的执行方式 -->
<bean id="cronQuartzClock" class="org.springframework.scheduling.quartz.CronTriggerBean" >
<property name="jobDetail">
<ref bean="quartzClock"></ref>
</property>
<property name="cronExpression">
<value>0/1 * * * * ? </value>
</property>
</bean>
<!--第三步 启动定时任务,注意这里的ref bean -->
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers"> <!--增加调度 triggers属性中,我们可以增加多个触发器 -->
<list>
<ref bean="cronQuartzClock"></ref>
</list>
</property>
</bean>
</beans>
3.执行类
package com.sgfm.datacenter.sys;
import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class DataTimingTask extends QuartzJobBean{
public DataTimingTask() {
super();
}
@Override <!-- 定时执行方法-->
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
// TODO Auto-generated method stub
System.out.println(new Date().toLocaleString());
}
}
- 在线定时cron表达式生成器网址: http://cron.qqe2.com/
- xml中配置进行读取applicationContext-quartz.xml
①、application-config.xml中配置:
<?xml version="1.0" encoding="utf-8"?>
<application>
<name>应用系统</name>
<organ>yyang</organ>
<spring>
<description>springbean配置,在web环境是没有意义的,在单元测试时可配置该项</description>
<context> //新增配置
applicationContext-*.xml,applicationConfig/*applicationContext*.xml,applicationConfig/**/*applicationContext*.xml,applicationConfig/**/**/*applicationContext*.xml
</context>
</spring>
</application>
②、web.xml中部分配置
<context-param>
<description>Spring ApplicationContext配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/applicationContext-*.xml //新增配置
classpath*:/applicationConfig/applicationContext.xml
classpath*:/applicationConfig/**/applicationContext-*.xml
classpath*:/applicationConfig/**/**/applicationContext-*.xml
classpath*:/applicationConfig/**/**/*applicationContext*.xml
</param-value>
</context-param>