Spring在3.0版本后集成了Spring-task,可以簡單實(shí)現(xiàn)定時(shí)任務(wù)。
下面簡單介紹一下Spring如何整合task
添加依賴pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.12</junit.version>
<spring.version>4.3.3.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
task的主要依賴在spring-context這個(gè)jar包中
DayJob.java
@Component
@Lazy(false)
public class DayJob {
@Scheduled(cron = "0/5 * * * * ?")
public void executeJob() {
System.out.println("Spring-task正在執(zhí)行:"+new Date());
}
}
這里面用到了Cron表達(dá)式,這個(gè)和quartz的表達(dá)式是一樣的
對(duì)于CornExpress講解如下:
字段 允許值 允許的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小時(shí) 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可選) 留空, 1970-2099 , - * /
表達(dá)式 意義
"0 0 12 * * ?" 每天中午12點(diǎn)觸發(fā)
"0 15 10 ? * *" 每天上午10:15觸發(fā)
"0 15 10 * * ?" 每天上午10:15觸發(fā)
"0 15 10 * * ? *" 每天上午10:15觸發(fā)
"0 15 10 * * ? 2005" 2005年的每天上午10:15觸發(fā)
"0 * 14 * * ?" 在每天下午2點(diǎn)到下午2:59期間的每1分鐘觸發(fā)
"0 0/5 14 * * ?" 在每天下午2點(diǎn)到下午2:55期間的每5分鐘觸發(fā)
"0 0/5 14,18 * * ?" 在每天下午2點(diǎn)到2:55期間和下午6點(diǎn)到6:55期間的每5分鐘觸發(fā)
"0 0-5 14 * * ?" 在每天下午2點(diǎn)到下午2:05期間的每1分鐘觸發(fā)
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44觸發(fā)
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15觸發(fā)
"0 15 10 15 * ?" 每月15日上午10:15觸發(fā)
"0 15 10 L * ?" 每月最后一日的上午10:15觸發(fā)
"0 15 10 ? * 6L" 每月的最后一個(gè)星期五上午10:15觸發(fā)
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一個(gè)星期五上午10:15觸發(fā)
"0 15 10 ? * 6#3" 每月的第三個(gè)星期五上午10:15觸發(fā)
特殊字符 意義
* 表示所有值;
? 表示未說明的值,即不關(guān)心它為何值;
- 表示一個(gè)指定的范圍;
, 表示附加一個(gè)可能值;
/ 符號(hào)前表示開始時(shí)間,符號(hào)后表示每次遞增的值;
L("last") ("last") "L" 用在day-of-month字段意思是 "這個(gè)月最后一天";用在 day-of-week字段, 它簡單意思是 "7" or "SAT"。 如果在day-of-week字段里和數(shù)字聯(lián)合使用,它的意思就是 "這個(gè)月的最后一個(gè)星期幾" – 例如: "6L" means "這個(gè)月的最后一個(gè)星期五". 當(dāng)我們用“L”時(shí),不指明一個(gè)列表值或者范圍是很重要的,不然的話,我們會(huì)得到一些意想不到的結(jié)果。
W("weekday") 只能用在day-of-month字段。用來描敘最接近指定天的工作日(周一到周五)。例如:在day-of-month字段用“15W”指“最接近這個(gè)月第15天的工作日”,即如果這個(gè)月第15天是周六,那么觸發(fā)器將會(huì)在這個(gè)月第14天即周五觸發(fā);如果這個(gè)月第15天是周日,那么觸發(fā)器將會(huì)在這個(gè)月第16天即周一觸發(fā);如果這個(gè)月第15天是周二,那么就在觸發(fā)器這天觸發(fā)。注意一點(diǎn):這個(gè)用法只會(huì)在當(dāng)前月計(jì)算值,不會(huì)越過當(dāng)前月?!癢”字符僅能在day-of-month指明一天,不能是一個(gè)范圍或列表。也可以用“LW”來指定這個(gè)月的最后一個(gè)工作日。
# 只能用在day-of-week字段。用來指定這個(gè)月的第幾個(gè)周幾。例:在day-of-week字段用"6#3"指這個(gè)月第3個(gè)周五(6指周五,3指第3個(gè))。如果指定的日期不存在,觸發(fā)器就不會(huì)觸發(fā)。
C 指和calendar聯(lián)系后計(jì)算過的值。例:在day-of-month 字段用“5C”指在這個(gè)月第5天或之后包括calendar的第一天;在day-of-week字段用“1C”指在這周日或之后包括calendar的第一天
applicationContext-job.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- 自動(dòng)掃描的包名 -->
<context:component-scan base-package="com.critc.job.spring" />
<task:annotation-driven /> <!-- 定時(shí)器開關(guān)-->
</beans>
這里面通過配置<task:annotation-driven />來控制定時(shí)任務(wù)的啟動(dòng)。
同時(shí)在beans的配置里面加了xmlns:task="http://www.springframework.org/schema/task"
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext-job.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
這一塊是web工程整合spring用到的,在contextConfigLocation參數(shù)里面加入了classpath:applicationContext-job.xml,啟動(dòng)時(shí)加載該配置。
啟動(dòng)tomcat,在控制臺(tái)輸出如下:

Spring-task實(shí)現(xiàn)