spring3.0 timer XML configuration and annotation methods

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.