1. Spring's 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> <!--Here is the execution of--> <task:scheduled ref= "Mytaskxml" method= "show" cron every five seconds. = "*/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, the timer task based on annotations
Package com.spring.task;
Import org.springframework.scheduling.annotation.Scheduled;
Import org.springframework.stereotype.Component;
/**
* Based on the annotation timer
* @author HJ/
@Component public
class Mytaskannotation {
/**
* timed calculation. Perform A/@Scheduled every 01:00
(cron = "0 0 1 * * *") public
Void Show () {
System.out.println ("Annotatio N:is show Run ");
}
/**
* Heartbeat update. Executes once at startup, and then executes 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");
}
Run 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 performs two bug reference links: http://nkliuliu.iteye.com/blog/816335
Reprint Link: http://blog.csdn.net/wxwzy738/article/details/25158787