spring定时器
开发工具:eclipse
以下Jar包放在lib目录下:aopalliance-1.0.jar 、commons-logging-1.2.jar、quartz-all-1.6.1.jar、spring-aop-4.2.0.RELEASE.jar、spring-beans-4.2.0.RELEASE.jar、spring-context-4.2.0.RELEASE.jar、spring-context-support-4.2.0.RELEASE.jar、spring-core-4.2.0.RELEASE.jar、spring-expression-4.2.0.RELEASE.jar、spring-web-4.2.0.RELEASE.jar、spring-webmvc-4.2.0.RELEASE.jar、spring-webmvc-portlet-4.2.0.RELEASE.jar、spring-websocket-4.2.0.RELEASE.jar
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">
<!-- 定义Spring MVC的前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 让Spring MVC的前端控制器拦截所有请求 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springmvc-config.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:mvc="http://www.springframework.org/schema/mvc"
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-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/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- 定时器开关-->
<task:annotation-driven />
<!-- 测试spring定时器是否能用 -->
<bean id="testSpringTimeService" class="com.blit.service.impl.TestSpringTimeServiceImpl"></bean>
<task:scheduled-tasks>
<!-- 每天5s执行一次 -->
<task:scheduled ref="testSpringTimeService" method="testQuart" cron="0/5 * * * * ?"/> /*百度spring定时器表达式*/
</task:scheduled-tasks>
</beans>
TestSpringTimeServiceImpl实现类:
package com.blit.service.impl;
public class TestSpringTimeServiceImpl {
public void testQuart(){
/*定时器所做的逻辑处理*/
System.out.println("Spring 定时器每5秒触发一次!!!!!!");
}
}
备注:spring定时器表达式生成网址:http://cron.qqe2.com/