一.web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application*.xml</param-value>
</context-param>
<listener>
<description>Spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
二.执行任务的普通Java类.
package com.test.service;
import org.springframework.stereotype.Service;
@Service("taskJob")
public class TaskJob {
public void job1(){
System.out.println("任务一进行中...");
}
public void job2(){
System.out.println("任务二进行中...");
}
public TaskJob() {
super();
System.out.println("业务类加载完成...");
}
}
三.applicationContext.xml.
<!-- 支持注解 -->
<context:annotation-config />
<!-- 启动自动扫描,不扫描@Controller,该组件由SpringMVC配置文件扫描 -->
<context:component-scan base-package="com.test.service" />
<task:scheduled-tasks>
<task:scheduled ref="taskJob" method="job1" cron="0/5 * * * * ?"/>
<task:scheduled ref="taskJob" method="job2" cron="0/5 * * * * ?"/>
</task:scheduled-tasks>
四.启动项目
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
九月 21, 2017 5:11:17 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\DevelopTool\apache-tomcat-7.0.78\webapps\ROOT has finished in 407 ms
九月 21, 2017 5:11:17 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
九月 21, 2017 5:11:17 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-bio-8009"]
九月 21, 2017 5:11:17 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 9425 ms
任务二进行中...
任务一进行中...
任务二进行中...
任务一进行中...
任务二进行中...