前言
在Spring MVC项目基础上使用Spring task,开发定时任务调度功能。
1、引入依赖
在Spring MVC项目基础上使用,首先引用Spring其他组件(context、core等)
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.13</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.13</version> </dependency>
2、环境配置
2.1、web.xml配置
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Spring MVC</display-name> <!--配置总控--> <servlet> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <!--配置 DispatcherServlet 的一个初始化参数:spring mvc 配置文件的位置和名称--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> </servlet> <!--配置映射路径--> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--配置字符编码--> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2.2、spring mvc 配置文件
<?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:mvc="http://www.springframework.org/schema/mvc" 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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd "> <!--开启扫描组件--> <context:component-scan base-package="com.xxx.springmvc"/> <!--注解驱动 --> <mvc:annotation-driven/> <!-- 注解定时任务 --> <task:annotation-driven/> <!--thymeleaf视图解析器--> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <!-- 视图前缀 --> <property name="prefix" value="/WEB-INF/static/"/> <!-- 视图后缀 --> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8"/> </bean> </property> </bean> </property> </bean> </beans>
3、 案例
import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Slf4j @Component public class Task { /** * 定时任务方法 */ //每隔 1 分钟执行一次:0 */1 * * * ? //每天 23 点执行一次:0 0 23 * * ? //每天中午12点触发 0 0 12 * * ? //每天上午10:15触发 0 15 10 ? * * 0 15 10 * * ? 0 15 10 * * ? //每月 1 号凌晨 1 点执行一次:0 0 1 1 * ? //每月最后一天 23 点执行一次:0 0 23 L * ? //每周星期天凌晨 1 点实行一次:0 0 1 ? * L //在 26 分、29 分、33 分执行一次:0 26,29,33 * * * ? //2005年的每天上午10:15触发 0 15 10 * * ? 2005 //在每天下午2点到下午2.59分期间的每1分钟触发 0 * 14 * * ? //cron 线上工具 : https://cron.qqe2.com/ @Scheduled(cron = "0/5 * * * * ?") public void send(){ log.info("start....."); } }