如下是Spring定时器的一种实现方式的测试,仅供参考java
一,建立maven工程SpringTask_Demo(war)web
二,pom.xml加入依赖和插件spring
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
</properties>apache
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</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-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
</dependencies>tomcat
<build>mvc
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>app
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 指定端口 -->
<port>9108</port>
<!-- 请求路径 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>maven
三,配置web.xml测试
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5"> ui
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
四,编写配置文件applicationContext-task.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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 http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd">
<context:property-placeholder location="classpath:application.properties"/>
<task:annotation-driven/>
<context:component-scan base-package="com.springTask.task"/>
</beans>
五编写测试类
import java.util.Date;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class SeckillTask {
@Value("${Trad_time01}")
private String Trad_time01;
/**
* 执行了任务调度
*/
/**
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev()
{
return new PropertySourcesPlaceholderConfigurer(); //加上以后spring才能识别${}中的内容
}
@Scheduled(cron="${Trad_time01}")这样写是读取配置文件里配置的定时器时间
*/
@Scheduled(cron="* * * * * ?")
public void refreshSeckillGoods(){
System.out.println("执行了任务调度"+new Date());
}
}
若定时器配置为配置文件的格式则
配置application.properties
#定时器1配置
Trad_time01=* * * * * ?
===========================================关于cron表达式的补充=======================
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每个域表明一个含义,Cron有以下两种语法格式:
(1)Seconds Minutes Hours DayofMonth Month DayofWeek Year
(2)Seconds Minutes Hours DayofMonth Month DayofWeek
每个域可出现的字符以下:
Seconds:可出现", - * /"四个字符,有效范围为0-59的整数
Minutes:可出现", - * /"四个字符,有效范围为0-59的整数
Hours:可出现", - * /"四个字符,有效范围为0-23的整数
DayofMonth:可出现", - * / ? L W C"八个字符,有效范围为1-31的整数
Month:可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc
DayofWeek:可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推
Year:可出现", - * /"四个字符,有效范围为1970-2099年
每个域都使用数字,但还能够出现以下特殊字符,它们的含义是:
(1)*:表示匹配该域的任意值,假如在Minutes域使用*, 即表示每分钟都会触发事件。
(2)?:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。由于DayofMonth和 DayofWeek会相互影响。例如想在每个月的20日触发调度,无论20日究竟是星期几,则只能使用以下写法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,若是使用*表示无论星期几都会触发,实际上并非这样。
(3)-:表示范围,例如在Minutes域使用5-20,表示从5分到20分钟每分钟触发一次
(4)/:表示起始时间开始触发,而后每隔固定时间触发一次,例如在Minutes域使用5/20,则意味着5分钟触发一次,而25,45等分别触发一次.
(5),:表示列出枚举值值。例如:在Minutes域使用5,20,则意味着在5和20分每分钟触发一次。
(6)L:表示最后,只能出如今DayofWeek和DayofMonth域,若是在DayofWeek域使用5L,意味着在最后的一个星期四触发。
(7)W: 表示有效工做日(周一到周五),只能出如今DayofMonth域,系统将在离指定日期的最近的有效工做日触发事件。例如:在 DayofMonth使用5W,若是5日是星期六,则将在最近的工做日:星期五,即4日触发。若是5日是星期天,则在6日(周一)触发;若是5日在星期一 到星期五中的一天,则就在5日触发。另一点,W的最近寻找不会跨过月份
(8)LW:这两个字符能够连用,表示在某个月最后一个工做日,即最后一个星期五。
(9)#:用于肯定每月第几个星期几,只能出如今DayofMonth域。例如在4#2,表示某月的第二个星期三。
0 0 10,14,16 * * ? 天天上午10点,下午2点,4点
0 0/30 9-17 * * ? 朝九晚五工做时间内每半小时
0 0 12 ? * WED 表示每一个星期三中午12点
"0 0 12 * * ?" 天天中午12点触发
"0 15 10 ? * *" 天天上午10:15触发
"0 15 10 * * ?" 天天上午10:15触发
"0 15 10 * * ? *" 天天上午10:15触发
"0 15 10 * * ? 2005" 2005年的天天上午10:15触发
"0 * 14 * * ?" 在天天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在天天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在天天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在天天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每一年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每个月15日上午10:15触发
"0 15 10 L * ?" 每个月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每个月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每个月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每个月的第三个星期五上午10:15触发