spring3的定时执行任务

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

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 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。