Spring Timer technology Terminator-implements spring timers in XML configuration

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.