一、构建shedule.xml
1、限时任务定时 bean
<bean name="userInfoServiceTimer_task"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 限时任务类-->
<property name="targetObject" ref="userInfoServiceTimer" />
<!-- 限时任务方法 -->
<property name="targetMethod" value="say" />
<!-- 设置同步执行,也就是等待另一个任务执行完成再执行这个定时器 -->
<property name="concurrent" value="false" />
</bean>
2、任务限时
<bean id="userInfoServiceTimer_Trigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="userInfoServiceTimer_task" />
<!-- 每?S秒执行 -->
<property name="cronExpression" value="0/2 * * * * ?" />
</bean>
3、启动调度器(触发器)
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="userInfoServiceTimer_Trigger" />
</list>
</property>
</bean>
4、完整shedule.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--限时任务定时 -->
<bean name="userInfoServiceTimer_task"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="userInfoServiceTimer" />
<!-- 限时任务方法 -->
<property name="targetMethod" value="say" />
<!-- 设置同步执行,也就是等待另一个任务执行完成再执行这个定时器 -->
<property name="concurrent" value="false" />
</bean>
<!-- 任务限时 -->
<bean id="userInfoServiceTimer_Trigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="userInfoServiceTimer_task" />
<!-- 每?S秒执行 -->
<property name="cronExpression" value="0/2 * * * * ?" />
</bean>
<!-- 启动调度器 -->
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="userInfoServiceTimer_Trigger" />
</list>
</property>
</bean>
</beans>
二、在spring.xml中引入shedule.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
default-lazy-init="false">
<context:component-scan base-package="com.yx"/>
<import resource="schedule.xml"/>
<context:annotation-config/>
</beans>
三、定时器作用的类和方法
1、接口
package com.yx.yzh.service;
import com.yx.yzh.entity.UserInfo;
public interface UserInfoService {
public void say();
}
2、实现类
package com.yx.yzh.service.impl;
import org.springframework.stereotype.Service;
import com.yx.yzh.entity.UserInfo;
import com.yx.yzh.service.UserInfoService;
@Service("userInfoServiceTimer")
public class UserInfoServiceImpl implements UserInfoService {
public void say() {
System.out.println("say hello!");
}
}
说明:启动服务器后,系统开始进行每隔一定时段的任务调度。