除了自定义的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. Yii学习

    今天保存出错,但是什么也没提示,所以就查了下,怎么输出错误 $model->save(); $model->getErrors(); 获取,刚插入数据的id $reply->save ...

  2. 源代码管理工具(上)-SVN基本使用

    ------------------------------------------------------SVN简介和搭建 ------------------------------------- ...

  3. 77.Android之代码混淆

    转载:http://www.jianshu.com/p/7436a1a32891 简介 作为Android开发者,如果你不想开源你的应用,那么在应用发布前,就需要对代码进行混淆处理,从而让我们代码即使 ...

  4. bzoj3926: [Zjoi2015]诸神眷顾的幻想乡

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  5. [IOS Tableview] cell自定义view显示错误问题

    问题介绍:按照tableviewcell的tag自定义cell的view显示的时候,会出现拖动时显示错误情况(在Tableview的范围超出屏幕范围需要滑动的情况下). 我做的是一个下载界面,我为了简 ...

  6. CentOS编译安装nodejs

    1. 从node.js官网下载最新版的node.js安装包,node.tar.gz wget https://nodejs.org/dist/v4.3.1/node-v4.3.1.tar.gz    ...

  7. Some Simple Models of Neurons

    Linear neuron: \[y=b+\sum\limits_i{x_i w_i}\] Binary threshold neuron: \[z = \sum\limits_i{x_i w_i}\ ...

  8. PL/0编译器(java version) – Scanner.java

    1: package compiler; 2:   3: import java.io.BufferedReader; 4: import java.io.FileNotFoundException; ...

  9. Linux系统被入侵后处理经历

    服务器托管在外地机房. 突然,频繁收到一组服务器ping监控不可达邮件,赶紧登陆zabbix监控系统查看流量状况. 可见流量已经达到了800M左右,肯定不正常,马上尝试SSH登陆系统,不幸的事,这种情 ...

  10. iOS内存管理个人总结

    一.变量,本质代表一段可以操作的内存,她使用方式无非就是内存符号化+数据类型 1.保存变量有三个区域: 1>静态存储区 2>stack 3>heap 2.变量又根据声明的位置有两种称 ...