1.xml Configuration method
Xml
<!--Configure Spring Listener and Profile paths--<context-param> <PARAM-NAME>CONTEXTCONFIGLOCATION</PARAM-NAME&G T <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </ Listener>
Spring configuration file
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:task= "http://www.springframework.org/schema/ Task "xsi:schemalocation=" Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/ Spring-task-3.0.xsd "<task:scheduled-tasks> <task:scheduled ref=" Taskjob "method=" job1 "cron=" 0 * * * * ?" /> </task:scheduled-tasks> <context:component-scan base-package= "Com.gy.mytask"/>
It's timed to start at 0 seconds per minute.
cron= "0 * * * *?"
Java class, method no return value
Package Com.gy.mytask;import Org.springframework.stereotype.Service, @Servicepublic class Taskjob {public void Job1 () { SYSTEM.OUT.PRINTLN ("Task in progress ... "); System.out.println (Thread.CurrentThread (). GetName ()); }}
2. How to Annotate
Spring configuration file
<context:annotation-config/> <!--can be removed--<context:component-scan base-package= "Com.gy.mytask"/ > <!--This configuration is enabled, spring recognizes @scheduled annotations--<task:annotation-driven/>
<context:annotation-config/>
The effect is to explicitly Spring Container Registration
Autowiredannotationbeanpostprocessor,Commonannotationbeanpostprocessor,
Persistenceannotationbeanpostprocessoras wellRequiredannotationbeanpostprocessorit4aBeanpostprocessor. Register this4a BeanpostprocessorFor your system to recognize the corresponding annotations.
so when using<context:component-scan/>, you can add<context:annotation-config/>removed.
Java class
Package Com.gy.mytask;import Org.springframework.scheduling.annotation.scheduled;import Org.springframework.stereotype.Service; @Service public class Taskjob {@Scheduled (cron = "0 * * * *?") public void Job1 () {System.out.println ("task in progress ... "); }}
The default is single thread
pool-1-thread-1 task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1 Task in progress ... Pool-1-thread-1
Reference article:
http://my.oschina.net/u/559635/blog/389558
Http://blog.sina.com.cn/s/blog_872758480100wtfh.html
This article is from the "bit accumulation" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1741155
spring3.0 timer XML configuration and annotation methods