在Spring中,InitializingBean和DisposableBean是两个标记接口,为Spring执行时bean的初始化和销毁某些行为时的有用方法。
  1. 对于Bean实现 InitializingBean,它将运行 afterPropertiesSet()在所有的 bean 属性被设置之后。
  2. 对于 Bean 实现了DisposableBean,它将运行 destroy()在 Spring 容器释放该 bean 之后。

示例

下面是一个例子,向您展示如何使用 InitializingBean 和 DisposableBean。一个 CustomerService bean来实现 InitializingBean和DisposableBean 接口,并有一个消息(message)属性。
package com.yiibai.customer.services;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean; public class CustomerService implements InitializingBean, DisposableBean
{
String message; public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public void afterPropertiesSet() throws Exception {
System.out.println("Init method after properties are set : " + message);
} public void destroy() throws Exception {
System.out.println("Spring Container is destroy! Customer clean up");
} }

File : applicationContext.xml

<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.5.xsd"> <bean id="customerService" class="com.yiibai.customer.services.CustomerService">
<property name="message" value="I'm property message" />
</bean> </beans>

执行它

package com.yiibai.common;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yiibai.customer.services.CustomerService; public class App
{
public static void main( String[] args )
{
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"}); CustomerService cust = (CustomerService)context.getBean("customerService"); System.out.println(cust); context.close();
}
}
该ConfigurableApplicationContext.close()将关闭该应用程序的上下文,释放所有资源,并销毁所有缓存的单例bean。它是只用于 destroy() 方的演示目的。

输出结果

Init method after properties are set : I'm property message
com.yiibai.customer.services.CustomerService@4090c06f
Spring Container is destroy! Customer clean up
afterPropertiesSet()方法被调用在 message 属性设置后,而 destroy()方法是在调用 context.close()之后;
 
建议:
不建议使用InitializingBean和DisposableBean的接口,因为它将你的代码紧耦合到 Spring 代码中。 一个更好的做法应该是在bean的配置文件属性指定init-method和destroy-method
 

Spring Bean InitializingBean和DisposableBean实例的更多相关文章

  1. Spring学习(十二)-----Spring Bean init-method 和 destroy-method实例

    实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...

  2. Spring Bean init-method 和 destroy-method实例

    在Spring中,可以使用 init-method 和 destroy-method 在bean 配置文件属性用于在bean初始化和销毁某些动作时.这是用来替代 InitializingBean和Di ...

  3. Spring的InitializingBean与DisposableBean方法

    在bean初始化的时候,将所有显示提供的属性设置完毕后调用这个方法 org.springframework.beans.factory.InitializingBean#afterProperties ...

  4. Spring Bean生命周期,好像人的一生。。

    大家好,我是老三,上节我们手撸了一个简单的IOC容器五分钟,手撸一个Spring容器!,这节我们来看一看Spring中Bean的生命周期,我发现,和人的一生真的很像. 简单说说IoC和Bean IoC ...

  5. Spring Bean 生命周期之destroy——终极信仰

    上一篇文章 Spring Bean 生命周期之我从哪里来 说明了我是谁? 和 我从哪里来? 的两大哲学问题,今天我们要讨论一下终极哲学我要到哪里去? 初始化 Spring Bean 有三种方式: @P ...

  6. 【Spring注解驱动开发】使用InitializingBean和DisposableBean来管理bean的生命周期,你真的了解吗?

    写在前面 在<[Spring注解驱动开发]如何使用@Bean注解指定初始化和销毁的方法?看这一篇就够了!!>一文中,我们讲述了如何使用@Bean注解来指定bean初始化和销毁的方法.具体的 ...

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

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

  8. Spring Bean Life Cycle Methods – InitializingBean, DisposableBean, @PostConstruct, @PreDestroy and *Aware interfaces

    Spring Beans are the most important part of any Spring application. Spring ApplicationContext is res ...

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

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

随机推荐

  1. Apache虚拟主机配置(多个域名访问多个目录)(转)

    Apache虚拟主机配置(多个域名访问多个目录) 为了方便管理虚拟主机,我决定使用一种方法,那就是修改httpd-vhosts.conf文件. 第一步首先要使扩展文件httpd-vhosts.conf ...

  2. Shp上传至Oracle Spatial

    1.下载shp2sdo,将shp文件拷贝至shp2sdo相同路径下,打开windows命令窗口,执行: shp2sdo shp文件名 表名 -i id -s 4326 -d 例如:shp2sdo ci ...

  3. 转:mysql日志(Windows下开启Mysql慢查询、通用日志)

    一.Windows下开启Mysql慢查询详解 //show variables like '%quer%';查询是否开启了慢查询!! 第一步:修改my.ini(mysql配置文件)  在my.ini中 ...

  4. Codeforces 445A Boredom(DP+单调队列优化)

    题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可 ...

  5. C++实践积累

    C++ STL vector 如何彻底清空一个vector? 实践证明,vector.clear()并不能把vector容量清空,只会让vector.size()变为零,依然很占内存.那如何让vect ...

  6. webStorage,离线缓存

    一.webStorage 1.目标 1.了解cookie的不足之处,引入webstorage的概念    2.学习并且掌握webstorage有哪两种    3.学习并且掌握sessionStorag ...

  7. 什么是VC、PE、LP、GP?

    天使基金主要关注原创项目构思和小型初创项目,投资规模大多在300万元以下:风险投资关注初创时期企业长期投资,规模在1000万元以下:私募股权投资主要关注3年内可以上市的成熟型企业. VC即ventur ...

  8. day6 random随机数模块

        random 我们经常看到网站的随机验证码,这些都是由随机数生成的,因此我们需要了解一下随机数的模块.如何生成随机数. random 生成随机数 random.random()    生成0- ...

  9. python开发学习-day03(set集合、collection系列 、深浅拷贝、函数)

    s12-20160116-day03 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...

  10. LoadRunner函数–lr_vuser_status_message

    http://wenku.baidu.com/link?url=KbKGldKUkam4VyH5Z2doesA0ovQpuwM9nX_SnVMtWjo6rJPxj9DqB51z_m1giMbVo5Db ...