In spring, there are two ways to implement the function of the timer, respectively, scheduled annotation and XML configuration, this blog will show how to use the scheduled notation in spring to implement the function of the timer, the code and the corresponding explanation is as follows:
Code 1-spring configuration file (applicationcontext.xml file):
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:task= "Http://www.springframework.org/schema/task" xmlns: context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/ Schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd/HTTP Www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd "><!-- Use annotation to automatically register beans--<context:annotation-config/> <!--to scan all classes in the package specified by the Base-package attribute, to be @component in the package , @Controller, @Service, and @respositor-modified classes complete bean creation and automatic dependency injection. The Base-package property specifies the class package that needs to be scanned, and all classes in the class package and its recursive child packages are processed. --<context:component-scan base-package= "Com.ghj"/> <!--set the size of the task performer pool--<tAsk:executor id= "Executor" pool-size= "5"/> <!--set Scheduler pool Size--<task:scheduler id= "Scheduler" pool-size = "Ten"/> <!--Configure Spring Timer--<task:annotation-driven executor= "executor" scheduler= "Scheduler" in a note-driven manner /></beans>
Note: Be aware of the 25 lines of code No. 05, 11, 12, 21, 23, and 6 in the code above.
Code 2--spring Timer test Class (Springtimertest.java file):
Package Com.ghj.packageoftimer;import Java.text.simpledateformat;import Java.util.date;import Org.springframework.scheduling.annotation.scheduled;import org.springframework.stereotype.component;/** * Spring Timer Test class * * @author Gao Yingjie */@Component//To complete bean creation and automatic Dependency injection public class springtimertest{ /** * Spring Timer test method * * @author Gao Yingjie * /@Scheduled (cron = "0 0/1 * * *?") The method is defined by the @scheduled annotation as a method of spring timed calls where cron is used to indicate when the method is called Public Void Test () { System.err.println (new SimpleDateFormat ("yyyy mm month DD Day HH" mm min ss sec "). Format (new Date ()));} }
Code 3--the class (Startspringtimer.java file) that loads the spring configuration file and starts the spring timer:
Package Com.ghj.packageoftest;import org.springframework.context.support.classpathxmlapplicationcontext;/** * Load Spring config file, start spring timer * * @author Gao Yingjie */public class Startspringtimer {public static void main (string[] args) {new C Lasspathxmlapplicationcontext ("Conf/spring/applicationcontext.xml"); SYSTEM.OUT.PRINTLN ("Load spring config file completed, Spring Timer successfully started!!!") ");}}
The advantages of spring timers are realized in this way :
"0 minutes to download the Java Project resource "
Spring Timer Technology Terminator--implementation of spring timers using scheduled annotations