First, the timer through the annotated way
1. Engineering structure
2, the required jar package
3. SPRING-CONFIG.XML,SPRINGMVC configuration file
<?xml version="1.0"encoding="UTF-8"? ><beans xmlns="Http://www.springframework.org/schema/beans"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"XMLNS:OXM="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/OXM"Xmlns:context="Http://www.springframework.org/schema/context"XMLNS:AOP="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx="Http://www.springframework.org/schema/tx"Xmlns:task="Http://www.springframework.org/schema/task"xsi:schemalocation="Http://www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/OXMhttp//www.springframework.org/schema/oxm/spring-oxm-3.0.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context-3.0.xsdhttp//Www.springframework.org/schema/txhttp//www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp//Www.springframework.org/schema/taskhttp//www.springframework.org/schema/task/spring-task-3.2.xsd "><!--notifies the spring container to assemble the bean with annotations--<context:annotation-config/> <!--notifies the spring container to find the annotated bean using the automatic scanning mechanism -<context:component-scanBase-package="com.*"/> <task:annotation-driven/> <!--timer Switch--<!--configuration Return page filter--<bean id="Viewresolver" class="Org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="Viewclass"value="Org.springframework.web.servlet.view.JstlView"/> <property name="prefix"Value="/"/> <property name="suffix"Value=". JSP"/> </bean></beans>
Note: The following is the need to introduce the header path:
xmlns:task= "Http://www.springframework.org/schema/task" xsi:schemalocation= "http// Www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.2.xsd "
Here is the timer switch configuration:
<task:annotation-driven/>
4. Web. XML, loading SPRINGMVC configuration
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:web= "Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "id=" webapp_id "version=" 3.0 "><display-name>spring</display-name> <!--SPRINGMVC configuration--<servlet> <s Ervlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param- Value>/web-inf/spring-config.xml</param-value> </init-param> <load-on-startup>1</loa d-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome- File>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file> Index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list></web-app>
5, Timercontroller.java
PackageCom.timer;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importorg.springframework.scheduling.annotation.Scheduled;Importorg.springframework.stereotype.Component; @Component Public classTimercontroller {/*** Executed every 20 seconds*/@Scheduled (fixedrate= 1000*20) Public voidprint () {SimpleDateFormat format=NewSimpleDateFormat ("Yyyy-mm-dd h:m:s"); System.out.println ("Timer:" +format.format (NewDate ())); } }
Note: You must have @component before using the annotation class
Second, the implementation of the timer through the configuration XML file
1. SPRING-CONFIG.XML,SPRINGMVC Configuration
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:OXM= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/OXM"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"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.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/OXMhttp//www.springframework.org/schema/oxm/spring-oxm-3.0.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context-3.0.xsdhttp//Www.springframework.org/schema/txhttp//www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp//Www.springframework.org/schema/taskhttp//www.springframework.org/schema/task/spring-task-3.2.xsd "><!--notifies the spring container to assemble the bean with annotations--<context:annotation-config/> <!--notifies the spring container to find the annotated bean using the automatic scanning mechanism -<context:component-scan base- Package= "com.*"/> <task:annotation-driven/> <!--timer Switch--<bean id= "Agentexceltask"class= "Com.timer.TimerController1"/> <task:scheduled-tasks> <task:scheduled ref= "Agentexceltask" method= " Print "cron=" 0/5 * * * *? " /> </task:scheduled-tasks> <!--configuration Return page filter-<bean id= "Viewresolver"class= "Org.springframework.web.servlet.view.UrlBasedViewResolver" > <property name= "Viewclass"value= "Org.springframework.web.servlet.view.JstlView"/> <property name= "prefix" value= "/"/> <proper Ty name= "suffix" value= ". jsp"/> </bean></beans>
Note: There are multiple creation beans and time configurations that are triggered every 5 seconds with the following code:
class= "Com.timer.TimerController1"/> <task:scheduled-tasks> <task:scheduled ref= " Agentexceltask "method=" print "cron=" 0/5 * * * *? " />
Note: method= "Print" indicates that the Print method in the TimerController1 class is triggered.
2, Timercontroller1.java
Package Com.timer; Import Java.text.SimpleDateFormat; Import java.util.Date; Public class TimerController1 { publicvoid print () { new SimpleDateFormat ("Yyyy-mm-dd h:m:s"); System.out.println ("timer1:" +format.format (new Date ())); } }
Note: Time Format reference
SPRINGMVC's Timer