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 XML configuration 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: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:task= "Http://www.springframework.org/schema/task" Xsi:sche malocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/s Pring-task.xsd "><bean id=" Jobdispatcher "class=" Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean "><property name=" TargetObject " ><!--specifying the Dispatch object--><bean class= "Com.ghj.packageoftimer.SpringTimerTest"/></property>< Property Name= "Targetmethod" value= "test"/><!--Specify the timer to execute that method in the dispatch object--><property name= "concurrent" value= " False "/><!--Configure the job to run--></bean><bean id=" Crontrigger "class=" in parallel Org.springframework.scheduling.quartz.CronTriggerBean "><property name= "Jobdetail" ref= "Jobdispatcher"/><property name= "cronexpression" value= "0 0/1 * * *?"/>< !--cronexpression is used to indicate when the method is called--></bean><bean class= " Org.springframework.scheduling.quartz.SchedulerFactoryBean "><property name=" triggers "><list>< !--can place one or more triggers by using the ref tag in the List tab--><ref bean= "Crontrigger"/></list></property></bean> </beans>
Code 2--spring Timer Test Class (Springtimertest.java file):
Package Com.ghj.packageoftimer;import java.text.simpledateformat;import java.util.date;/** * Spring Timer TEST class * * @ Author Gao Yingjie */public class springtimertest{/** * Spring Timer test method * * @author Gao Yingjie * * Public Void Test () { system.er R.println (New SimpleDateFormat ("yyyy mm 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!!!") ");}}
Spring Timer technology Terminator-implements spring timers in XML configuration