自己用spring3.2+quartz+maven 整合一个小小的demo.仅供自己学习!
1. 在HelloController 中注入 IPersionService 接口,并调用PersionService 的save() 方法。
2. 在 application-context-quartz 中配置quartz 和Spring。
3. 在web.xml配置监听和application-context-quatz.xml
4:具体如下
/**
* @author
* @date 创建时间:2016-1-5 上午10:01:26
* @version 1.0
*/
@Controller
@RequestMapping(value = "/hello")
public class HelloController {
@Autowired
private IPersionService iPersionService;
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
@RequestMapping(value = "/say", method = RequestMethod.GET)
public void show(){
iPersionService.save();
}
}
/**
* @author
* @date 创建时间:2016-1-5 下午2:39:51
* @version 1.0
*/
@Service
public class PersionService implements IPersionService {
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
public void save() {
System.out.println("save:"+dateFormat.format(new Date()));
System.err.println("invok persionService save method !");
}
}
/**
* @author
* @date 创建时间:2016-1-5 下午2:37:43
* @version 1.0
*/
public interface IPersionService {
public void save();
}
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.0.xsd">
<context:component-scan base-package="com.sys">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
<!-- 自定义需要执行定时任务的类 -->
<bean id="quartzJob" class="com.sys.service.PersionService" />
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobTask"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="quartzJob" />
</property>
<property name="targetMethod">
<value>save</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="doTime"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="jobTask" />
</property>
<property name="cronExpression">
<value>0/30 * * * * ?</value>
<!-- <value>0 0/5 * * * ?</value> -->
</property>
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Spring config start -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/application-context-quartz.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring config end -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --> <bean id="startQuartz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="doTime" /> </list> </property> </bean> </beans>


本文详细介绍了如何利用Spring3.2、Quartz和Maven整合创建一个简单的示例应用,包括在控制器中注入服务接口、配置Quartz和Spring,以及在web.xml中配置监听和application-context-quartz.xml文件。
1299

被折叠的 条评论
为什么被折叠?



