Spring callback method Disposablebean interface

Source: Internet
Author: User

In addition to the custom Destroy-method. You can also implement the Disposablebean interface, the method that executes when the bean is destroyed, the interface has a destroy method, the life cycle is destroy----bean destruction---custom Destroy method

Simplebean.java

?
1234567891011121314151617181920212223242526272829303132333435 packagech5.destroy;importorg.springframework.beans.factory.DisposableBean;importorg.springframework.beans.factory.InitializingBean;publicclass SimpleBean implementsInitializingBean,DisposableBean {  publicvoidafterPropertiesSet() throwsException {        System.out.println("this is info from afterpropertiesSet from SimpleBean");    }privateString name;  privateString sex;  privateString age;  publicvoiddestroyMethod(){      System.out.println("this is info from customer destroy method");  }  publicvoiddestroy(){      System.out.println("this is info from destroy method");  }publicString getAge() {    returnage;}publicvoidsetAge(String age) {    this.age = age;}publicString getName() {    returnname;}publicvoidsetName(String name) {    this.name = name;}publicString getSex() {    return sex;}publicvoidsetSex(String sex) {    this.sex = sex;}}

Configuration file:

?
1234567891011121314151617 <?xmlversion="1.0" encoding="UTF-8"?><beans    xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><beanid="SimpleBean"class="ch5.destroy.SimpleBean"destroy-method="destroyMethod"><property name="name">    <value>gaoxiang</value>  </property><propertyname="sex">   <value>male</value></property><propertyname="age">    <value>26</value>  </property></bean></beans>

Test code:

?
1234567891011121314151617181920212223242526272829303132 package ch5.destroy;importjava.io.File;importorg.springframework.beans.factory.BeanFactory;importorg.springframework.beans.factory.config.ConfigurableListableBeanFactory;importorg.springframework.beans.factory.support.BeanDefinitionRegistry;importorg.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.FileSystemResource;publicclassTestSpring{  publicstaticvoidmain(String args[])  throwsException{      //获取bean factory      ConfigurableListableBeanFactory factory=(ConfigurableListableBeanFactory)getBeanFactory();  //使用子beanFactory        SimpleBean bean1=(SimpleBean)factory.getBean("SimpleBean");            System.out.println("before destroy");      factory.destroySingletons();      System.out.println("after destory");      }  publicstaticBeanFactory getBeanFactory(){      //获取bean factory      String realpath="";            //加载配置项      realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch5/destroy"+File.separator+"applicationContext.xml";             ConfigurableListableBeanFactory  factory=newXmlBeanFactory(newFileSystemResource(realpath));            returnfactory;  }}

Results:

This is info from Afterpropertiesset from Simplebean
Before destroy
This is info from Destroy method
This is info from customer Destroy method
After Destory

Original address: http://blog.csdn.net/daryl715/article/details/1678986

Spring callback method Disposablebean interface

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.