码迷,mamicode.com
首页 > 编程语言 > 详细

spring3的定时执行任务

时间:2014-11-24 11:33:52      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   sp   java   文件   on   

一个配置文件+一个类搞定:

1.配置文件,一个

<task:scheduled-tasks>标签,ref代表执行的类,method是方法,
cron="0 0/2 * * * ?"
每两分钟执行一次,延迟0秒
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
 3     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 4         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
 5         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
 6         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
 7         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
 8         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
 9         http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
10         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
11         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
12         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
13         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
14     <!-- cron表达式:*(秒0-59) *(分钟0-59) *(小时0-23) *(日期1-31) *(月份1-12或是JAN-DEC) *(星期1-7或是SUN-SAT) --> 
15      <task:scheduled-tasks>
16         <task:scheduled ref="taskComponent" method="LogTest" cron="0 0/2 * * * ?" />
17     </task:scheduled-tasks> 
18 </beans>

java类

 1 import org.apache.log4j.Logger;
 2 import org.springframework.beans.factory.annotation.Autowired;
 3 import org.springframework.stereotype.Component;
 4 import com.test.TestService
 5 
 6 @Component
 7 public class TaskComponent
 8 {
 9     @Autowired
10     private TestService testService;
11     static Logger logger = Logger.getLogger(TaskComponent.class);
12     
13     
14     
15     public void LogTest() 
16     {
17         //定义一个Log对象
18         Log log = new log();
19         System.out.print("每两分钟执行一次,延迟0秒");
20         testService.save(log);
21     }
22     
23 }

 

spring3的定时执行任务

标签:style   blog   http   io   color   sp   java   文件   on   

原文地址:http://www.cnblogs.com/liyangxj/p/4118112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!