Spring Task Demo .

一.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
任务二进行中...
任务一进行中...
任务二进行中...
任务一进行中...
任务二进行中...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值