本文转载自:http://www.cnblogs.com/xiaochangwei/p/5425618.html
第一种配置方式:这个比较复杂,配置的地方有点多
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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"
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.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 每隔一个小时重新获取一次token -->
<!-- 1.要调用的工作类 -->
<bean id="refreshAccessTokenJob" class="com.xiao.weixin.quartz.RefreshAccessToken"/>
<!-- 2.定义调用对象和调用对象的方法 -->
<bean id="refreshAccessTokenTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="refreshAccessTokenJob"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>work</value>
</property>
</bean>
<!-- 3.定义触发时间 -->
<bean id="refreshAccessToken" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="refreshAccessTokenTask"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>0 0 */2 * * ?</value>
</property>
</bean>
<!-- 4.总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="refreshAccessToken"/>
</list>
</property>
</bean>
</beans>
第二种配置方式:强烈推荐的这种
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<task:scheduled-tasks>
<task:scheduled ref="quartzTestBean" method="quartzJobTestMethod" cron="*/5 * * * * ?" />
</task:scheduled-tasks>
<bean id="quartzTestBean" class="xiaochangwei.zicp.net.service.QuartzTestServiceImpl"/>
</beans>
maven配置中加上
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
实现类
package xiaochangwei.zicp.net.service;
import java.util.Date;
import org.springframework.stereotype.Service;
import xiaochangwei.zicp.net.common.tools.DateUtil;
@Service
public class QuartzTestServiceImpl implements QuartzTestService {
public void quartzJobTestMethod() {
System.out.println("定时任务执行:" + DateUtil.getFormatDate(new Date()));
}
}
每隔五秒打印一个当前时间
执行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/48a2bd783472b78db0d1d1d9fbc99b36.png)
定外配置任务多久执行也很简单:
<task:scheduled ref="quartzTestBean" method="quartzJobTestMethod" cron="*/5 * * * * ?" />