Spring Task timed Task implementation

Source: Internet
Author: User
Tags dateformat

First, the introduction of spring-related jar package:

Ii. configuring Spring in Web. xml

<Listener>    <Description>Spring Listener</Description>    <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class></Listener><Context-param>    <Param-name>Contextconfiglocation</Param-name>    <Param-value>Classpath:applicationContext.xml</Param-value></Context-param>

Third, configure the listener in Applicationcontext.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:task= "Http://www.springframework.org/schema/task"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsd Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/ Spring-task-3.0.xsd "Default-lazy-init= "false">    <!--Annotation Method -    <Context:annotation-config/>    <Context:component-scanBase-package= "Com.test.task" />    <Task:annotation-driven/>        <!--XML Mode -    <!--<bean name= "Testtask" class= "Com.test.task.TestTask" lazy-init= "false" ></bean> <task: scheduled-tasks> <task:scheduled ref= "Testtask" method= "print" cron= "0/5 * * * *?" /> </task:scheduled-tasks> -</Beans>

Iv. Preparation of entity classes

 PackageCom.test.task;ImportJava.text.DateFormat;Importjava.util.Date;Importorg.springframework.scheduling.annotation.Scheduled;Importorg.springframework.stereotype.Component; @Component Public classtesttask {@Scheduled (cron= "*/5 * * * *?")     Public voidprint () {String time= Dateformat.getdatetimeinstance (). Format (NewDate ()); System.out.println ("Timer trigger print" +Time ); }}

V. Engineering Catalogue:

Operation Result:

Spring Task timed Task implementation

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.