除了自定义的destroy-method.还可以实现DisposableBean接口,来回调bean销毁时候执行的方法,这个接口有一个destroy方法,生命周期是是destroy----bean销毁---自定义的destroy方法

SimpleBean.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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;
}
}

配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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>

测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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 destory

原文地址:http://blog.csdn.net/daryl715/article/details/1678986

Spring回调方法DisposableBean接口的更多相关文章

  1. spring调用方法(接口和多个实现类的情况)

    以spring框架注入bean说明接口TestService 有2个实现类 TestServiceImp1 @Service("TestService1") ,TestServic ...

  2. spring bean初始化及销毁你必须要掌握的回调方法

    spring bean在初始化和销毁的时候我们可以触发一些自定义的回调操作. 初始化的时候实现的方法 1.通过java提供的@PostConstruct注解: 2.通过实现spring提供的Initi ...

  3. spring InitializingBean和DisposableBean init-method 和destroy-method @PostConstruct @PreDestroy

    对于初始化函数: @PostConstruct 注解的方法 InitializingBean接口定义的回调afterPropertiesSet() Bean配置中自定义的初始化函数 对于析构则与上相同 ...

  4. Spring中Bean的生命中期与InitializingBean和DisposableBean接口

    Spring提供了一些标志接口,用来改变BeanFactory中的bean的行为.它们包括InitializingBean和DisposableBean.实现这些接口将会导致BeanFactory调用 ...

  5. Spring钩子方法和钩子接口的使用详解

    本文转自:http://www.sohu.com/a/166804449_714863 前言 SpringFramework其实具有很高的扩展性,只是很少人喜欢挖掘那些扩展点,而且官方的Refrenc ...

  6. Spring源码学习(六)-spring初始化回调方法源码学习

    1.spring官方指定了三种初始化回调方法 1.1.@PostConstruct.@PreDestory 1.2.实现 InitializingBean DisposableBean 接口 1.3. ...

  7. Spring Bean生命周期回调方法

    参阅官方文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory ...

  8. Spring bean 实现InitializingBean和DisposableBean接口实现初始化和销毁前操作

    # InitializingBean接口> Spring Bean 实现这个接口,重写afterPropertiesSet方法,这样spring初始化完这个实体类后会调用这个方法```@Over ...

  9. fragment Activity之间传值的方法 之------------接口回调

    首先  定义一个  回调接口 public interface FragmentCallBack { public void callbackFun1(Bundle arg); public void ...

随机推荐

  1. MySQL的Sleep进程

    php的垃圾回收机制,其实只针对于php本身. 对于mysql,php没权利去自动去释放它的东西. 如果你在页面执行完毕前不调用mysql_close(),那么mysql那边是不会关闭这个连接的. 如 ...

  2. 学习WebSocket(一):Spring WebSocket的简单使用

    1.什么是websocket? WebSocket协议定义了一种web应用的新功能,它实现了服务器端和客户端的全双工通信.全双工通信即通信的双方可以同时发送和接收信息 的信息交互方式.它是继Java ...

  3. 【poj3017】 Cut the Sequence

    http://poj.org/problem?id=3017 (题目链接) 题意 给出一个数列要求将它分割成许多块,每块的数的和不超过m,要求每块中最大的数之和最小. Solution 这道题真的很不 ...

  4. HDU4612 Warm up

    Time Limit: 5000MS   Memory Limit: 65535KB   64bit IO Format: %I64d & %I64u Description N planet ...

  5. TcpClient类与TcpListener类

    TcpClient类 //构造方法1 TcpClient t = new TcpClient(); t.Connect(); //构造方法2 IPEndPoint iep = ); TcpClient ...

  6. Python – locals和globals

    转载: Python两个内置函数--locals 和globals (学习笔记) Python两个内置函数locals 和globals, 这两个函数主要提供,基于字典的访问局部和全局变量的方式.在理 ...

  7. JBoss AS7 快速配置

    作者:MinUnix  原文出处:http://www.minunix.com/2013/08/jboss-as7-01/  如需转载请注明出处! 文档下载:http://www.minunix.co ...

  8. linux网络配置命令

    ifconfig 命令命令功能ifconfig命令被用于配置和显不Linux内核中网络接口的网络参数.命令语法ifconfig (参数)参数说明add〈地址〉:设置网络设备IPv6的P地址;del〈地 ...

  9. jQuery知识点总结(第五天)

    节点的操作和数据库操作一样,无非是增.删.改.查. 今天总结删除节点.复制节点.替换节点.与包裹节点 删除节点: 如果文档中一个元素多余,那么就需要删除掉.jQuery提供了三种删除节点的方法.分别是 ...

  10. ubuntu sublime text3 lisence

    help --> enter lisence ----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-855605 813 ...