Spring Bean InitializingBean和DisposableBean实例

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

示例

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

File:CustomerService.java

public class CustomerService implements InitializingBean, DisposableBean{

	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("Spring Container is destroy! Customer clean up");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("Init method after properties are set : " + message);
	}

	@Override
	public String toString() {
		return "CustomerService [message=" + message + "]";
	}
}

File:beans.xml

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

执行程序:

public class Test {

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

输出结果

二月 22, 2018 10:29:03 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3fa77460: startup date [Thu Feb 22 22:29:03 CST 2018]; root of context hierarchy
二月 22, 2018 10:29:03 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
Init method after properties are set : I'm property message
CustomerService [message=I'm property message]
二月 22, 2018 10:29:03 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@3fa77460: startup date [Thu Feb 22 22:29:03 CST 2018]; root of context hierarchy
Spring Container is destroy! Customer clean up
afterPropertiesSet()方法被调用在 message 属性设置后,而 destroy()方法是在调用 context.close()之后;


建议:
不建议使用InitializingBean和DisposableBean的接口,因为它将你的代码紧耦合到 Spring 代码中。 一个更好的做法应该是在bean的配置文件属性指定 init-method和destroy-method


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值