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