Spring回調方法DisposableBean接口

除了自定義的destroy-method.還能夠實現DisposableBean接口,來回調bean銷燬時候執行的方法,這個接口有一個destroy方法,生命週期是是destroy----bean銷燬---自定義的destroy方法 java

 

SimpleBean.java
web

package ch5.destroy;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class SimpleBean implements InitializingBean,DisposableBean {
  public void afterPropertiesSet() throws Exception {
        System.out.println("this is info from afterpropertiesSet from SimpleBean");
    }
private  String name;
  private String sex;
  private String age;
  public void destroyMethod(){
      System.out.println("this is info from customer destroy method");
  }
  public void destroy(){
      System.out.println("this is info from destroy method");
  }
public String getAge() {
    return age;
}
public void setAge(String age) {
    this.age = age;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getSex() {
    return sex;
}
public void setSex(String sex) {
    this.sex = sex;
}
}

配置文件:
spring

<?xml version="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">
<bean id="SimpleBean" class="ch5.destroy.SimpleBean" destroy-method="destroyMethod">
<property name="name">
    <value>gaoxiang</value>
  </property>
<property name="sex">
   <value>male</value>
</property>
<property name="age">
    <value>26</value>
  </property>
</bean>
</beans>

測試代碼:
app

package ch5.destroy;
import java.io.File;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestSpring{
  public static void main(String args[])  throws Exception{
      //獲取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");
    
  }
  public static BeanFactory getBeanFactory(){
      //獲取bean factory
      String realpath="";
   
         //加載配置項
      realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch5/destroy"+File.separator+"applicationContext.xml";
  
     
      ConfigurableListableBeanFactory  factory=new XmlBeanFactory(new FileSystemResource(realpath));
      
      return factory;
  }
}
 

結果:測試

this is info from afterpropertiesSet from SimpleBean
before destroy
this is info from destroy method
this is info from customer destroy method

after destorythis

相關文章
相關標籤/搜索