1. Spring Configuration file
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:p=" http://www.springframework.org/schema/p "xmlns:task=" http://www.springframework.org /schema/task "xmlns:context=" Http://www.springframework.org/schema/context "xmlns:aop="/HTTP/ Www.springframework.org/schema/aop "xsi:schemalocation=" Http://www.springframework.org/schema/beans http://ww W.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/tx Http://www.spri Ngframework.org/schema/tx/spring-tx-3.0.xsd Http://www.springframework.org/schema/jee http://www.spring Framework.org/schema/jee/spring-jee-3.0.xsd Http://www.springframework.org/schema/context HTTP://WWW.SP Ringframework.org/schema/context/spring-context-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http: Www.springframework.org/schema/aop/spring-aop-3.0.xsd Http://www.sprinGframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd "> <task:ann
Otation-driven/> <!--timer Switch--<bean id= "Mytaskxml" class= "com.spring.task.MyTaskXml" ></bean> <task:scheduled-tasks> <!--This is done every five seconds--<task:scheduled ref= "Mytaskxml" method= "show" cron = "*/5 * * * * *?"/> <task:scheduled ref= "mytaskxml" method= "print" cron= "*/10 * * * *?" /> </task:scheduled-tasks> <!--automatically scanned package name--<context:component-scan base-package= "com . Spring.task "/> </beans>
2. XML-based Timer task
Package com.spring.task;
/**
* XML-based timer
* @author HJ
*
/public class Mytaskxml {public
void Show () {
System.out.println ("Xml:is show Run");
public void print () {
System.out.println ("Xml:print Run");
}
}
3, note-based timer task
Package com.spring.task;
Import org.springframework.scheduling.annotation.Scheduled;
Import org.springframework.stereotype.Component;
/**
* Note-based timer
* @author HJ */
@Component Public
class Mytaskannotation {
/**
* timed calculation. Once daily 01:00 *
/
@Scheduled (cron = "0 0 1 * * *") public
Void Show () {
System.out.println ("Annotatio N:is show Run ");
/**
* Heartbeat update. Executes once at startup, and executes once every 2 seconds
*/
@Scheduled (fixedrate = 1000*2) public
void print () {
System.out.println (" Annotation:print run ");
}
}
4. Testing
Package com.spring.test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {public
static void Main (string[] args) {
ApplicationContext ctx = new Classpathxmlapplicationc Ontext ("Spring-mvc.xml");
}
}
Operation Result:
Annotation:print Run
Annotation:print Run
Annotation:print Run
Xml:print Run
Xml:is Show Run
Annotation:print Run
Annotation:print Run
Spring timed task Execution two times bug reference link: http://nkliuliu.iteye.com/blog/816335
Reprint Link: http://blog.csdn.net/wxwzy738/article/details/25158787