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