首先需要建立几个文件:
需要在pom中添加依赖
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config.properties</value>
<value>classpath:quartz.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"></property>
</bean>
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="com.ahxh">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<import resource="spring-mybatis.xml"/>
<import resource="quartz.xml"/>
</beans>
spring-mvc.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<context:component-scan base-package="com.anxh.quartz" />
<mvc:default-servlet-handler/>
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="com.ahxh.controller">
</context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true">
</aop:aspectj-autoproxy>
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<!-- 支持JSON数据格式 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/><!-- 解析导出文件byte流 -->
<ref bean="fastJsonHttpMessageConverter"/>
<!--
<ref bean="mappingJacksonHttpMessageConverter" />
-->
</list>
</property>
</bean>
<!-- 使用fastJson来支持JSON数据格式 -->
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
</list>
</property>
<property name="features">
<list>
<value>WriteMapNullValue</value>
<value>QuoteFieldNames</value>
</list>
</property>
</bean>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
</bean>
</beans>
quartz.properties
#\u67E5\u8BE2\u65E5\u671F\u662F\u5426\u53D8\u5316\u8868\u8FBE\u5F0F\u610F\u601D(10/10 * * * * ? 10\u79D2\u540E\u6BCF\u969410\u79D2\u8FD0\u884C)
qzExcutTimer=5/5 * * * * ?
#\u65E5\u7EC8\u542F\u52A8\u65F6\u95F4(0 0 18 * * ?\u8868\u793A\u6BCF\u5929\u4E0B\u53486\u70B9\u6267\u884C)
qzRunDataTimer=0 32 11 * * ?
quartz.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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:mybatis="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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 ">
<bean id="quartzJob" class="com.ahxh.quartz.QuartzJob"></bean>
<!-- 使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job借口,通过targetMethod指定调用方法 -->
<bean id="JobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 要调度的对象 -->
<property name="targetObject" ref="quartzJob" />
<!-- 要执行的方法名称 -->
<property name="targetMethod" value="excutor" />
<!-- 将并发设置为false -->
<property name="concurrent" value="false"></property>
</bean>
<!-- 调度触发器 -->
<bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="JobTask" />
<!-- 表达式 -->
<property name="cronExpression" value="${qzExcutTimer}" />
</bean>
<!-- 调度工厂:如果将lazy-init="false",那么容器启动就会执行调度程序 -->
<bean id="startQuartz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- 作业调度器,list下可加入其它的调度器 -->
<ref bean="trigger" />
</list>
</property>
</bean>
</beans>
在web.xml中加入
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>quartz.root</param-value>
</context-param>
最后,根据配置文件创建个方法,定时执行:
例:
package com.ahxh.quartz;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.ahxh.domain.User;
import com.ahxh.service.UserService;
public class QuartzJob {
@Autowired
@Qualifier("userService")
private UserService userService;
public void excutor(){
List<User> users=userService.getAllUsers();
for(int i=0;i<users.size();i++){
System.out.println("user========"+users.get(i));
}
}
}
原博客:https://blog.csdn.net/qq_35029429/article/details/78508002