Use of Spring Init-method and Destroy-method

Source: Internet
Author: User

Use of Spring Init-method and Destroy-method

 

  Spring provides both the Init-method and Destroy-method properties, which need to be loaded in the bean node, to accommodate some of the business that the developer needs to do before or after the execution of a certain method.

In the code section below, for completeness, I've added IOC and dependency injection

First, we create an interface Studentservice.java

1  PackageCn.demo.service;2 3 /**4 * Interface5  * @authorXkjava6 * @date 2015-07-127  */8  Public InterfaceStudentservice {9     Ten      Public voidhellospring (String str); One  A}

  

Second,Studentserviceimpl.java realization Class

1  PackageCn.demo.service.impl;2 3 Importjava.io.Serializable;4 5 ImportCn.demo.service.StudentService;6 7  Public classStudentserviceimplImplementsstudentservice,serializable{8 9     Ten     /** One      *  A      */ -     Private Static Final LongSerialversionuid = 6130145558179499205L; -  the      -     /** - * Demo Test -      */ + @Override -      Public voidhellospring (String str) { +         //TODO auto-generated Method Stub ASystem.err.println ("Do this here") +str); at     } -      -     /** - * Execute hellospring before executing -      */ -      Public voidinits () { inSystem.err.println ("This is done before executing hellospring!") "); -     } to      +      -     /** the * Called before destroying objects *      */ $      Public voidshutdown () {Panax NotoginsengSYSTEM.ERR.PRINTLN ("Call Shutdown () method before destroying Studentservice object instance")); -     } the  +}

Three, Spring XML file

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"4 Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:context= "Http://www.springframework.org/schema/context"5 xsi:schemalocation="  6 Http://www.springframework.org/schema/context7 http://www.springframework.org/schema/context/spring-context-3.0.xsd8 Http://www.springframework.org/schema/beans9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsdTen Http://www.springframework.org/schema/tx One http://www.springframework.org/schema/tx/spring-tx-3.0.xsd A HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP - http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> -          the     <!--Note the method names and Studentserviceimpl.java that are loaded by Init-method and Destroy-method are compared -     -     <BeanID= "Stu"class= "Cn.demo.service.impl.StudentServiceImpl"Init-method= "Inits"Destroy-method= "Shutdown"></Bean> -  - </Beans>

Four, Testdemo.java test class

1  Packagecn.demo.test;2 3 Importjava.io.Serializable;4 5 Importorg.springframework.beans.factory.annotation.Autowired;6 ImportOrg.springframework.context.ApplicationContext;7 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;8 9 ImportCn.demo.service.StudentService;Ten  One /** A * Test -  * @authorXkjava -  * the  */ -  Public classTestdemoImplementsSerializable { -  -      +     /** -      *  +      */ A     Private Static Final LongSerialversionuid = 6343872716391435079L; at      -  -     /** - * Main entrance -      */ -      Public Static voidMain (string[] args) { in          -          toApplicationContext context =NewClasspathxmlapplicationcontext (Newstring[]{"Applicationcontext.xml"}); +          -Studentservice Studentservice = Context.getbean ("Stu", Studentservice.class); the          *Studentservice.hellospring ("hello! spring! "); $         Panax Notoginseng         //destroying Studentservice instance objects - ( (Classpathxmlapplicationcontext) context). Close ();  the          +     } A}

Attention:

after the execution is complete , you need to   ( (Classpathxmlapplicationcontext) context). Close (); Close to execute Destroy-method

Use of Spring Init-method and Destroy-method

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.