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