The Springtask of spring task scheduling based on XML and annotation-based usage examples

Source: Internet
Author: User

The environmental requirements for using spring are: JDK1.8 above, Maven3.0 above. Maven Dependency

The Springtask is integrated in the Springcontext, so only springcontext is required.

You can use Maven-compiler-plugin to explicitly specify the JDK version.

<Dependency>    <groupId>Org.springframework</groupId>    <Artifactid>Spring-context</Artifactid>    <version>4.3.12.RELEASE</version></Dependency><Build>    <Plugins>        <plugin>            <groupId>Org.apache.maven.plugins</groupId>            <Artifactid>Maven-compiler-plugin</Artifactid>            <version>3.5.1</version>            <Configuration>                <Source>1.8</Source>                <Target>1.8</Target>            </Configuration>        </plugin>    </Plugins></Build>
Spring XML configuration (XML-based)
<?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.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.1.xsd Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/ Spring-task-4.1.xsd ">    <!--Configuring annotation Scans -    <Context:component-scanBase-package= "Com.cky.schadule"/>    <Task:schedulerID= "TaskScheduler"pool-size= "+" />    <Task:scheduled-tasksScheduler= "TaskScheduler">        <!--trigger once per 30s -        <task:scheduledref= "Schaduledemo"Method= "SayHello1"Cron= "0/5 * * * *?"/>        <!--trigger once per 10s -        <task:scheduledref= "Schaduledemo"Method= "SayHello2"Cron= "0/10 * * * *?"/>    </Task:scheduled-tasks></Beans>
Task class
@Component Public classSchaduledemo { Public voidSayHello1 () {System.out.println ("Task1:hello~ now Time" +Localtime.now ()); }     Public voidSayHello2 () {System.out.println ("Task2:hello~ now time IS0" +Localtime.now ()); }     Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Classpath:/application-context.xml"); }}
Spring.xml configuration (based on annotations)
<?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.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.1.xsd Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/ Spring-task-4.1.xsd ">    <!--Configuring annotation Scans -    <Context:component-scanBase-package= "Com.cky.schadule"/>    <Task:schedulerID= "TaskScheduler"pool-size= "+" />    <!--Turn on annotation drivers -    <Task:annotation-drivenScheduler= "TaskScheduler"/></Beans>
Task class
@Component Public classSchaduledemo {@Scheduled (cron= "0/5 * * * *?")     Public voidSayHello1 () {System.out.println ("Task1:hello~ now Time" +Localtime.now ()); } @Scheduled (Cron= "0/10 * * * *?")     Public voidSayHello2 () {System.out.println ("Task2:hello~ now time IS0" +Localtime.now ()); }     Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Classpath:/application-context.xml"); }}

The Springtask of spring task scheduling based on XML and annotation-based usage examples

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.