Spring InitializingBean和DisposableBean示例

在Spring中, InitializingBeanDisposableBean是两个标记接口,这是Spring在bean初始化和销毁​​时执行某些操作的有用方式。

  1. 对于实现了bean的InitializingBean,它将在设置所有bean属性之后运行afterPropertiesSet()
  2. 对于由bean实现的DisposableBean,它将在Spring容器释放bean之后运行destroy()

这是一个示例,向您展示如何使用InitializingBeanDisposableBean 。 一个CustomerService bean,用于实现InitializingBean和DisposableBean接口,并具有message属性。

package com.mkyong.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");
	}
	
}

文件:Spring-Customer.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.mkyong.customer.services.CustomerService">
		<property name="message" value="i'm property message" />
       </bean>
		
</beans>

运行

package com.mkyong.common;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mkyong.customer.services.CustomerService;

public class App 
{
    public static void main( String[] args )
    {
    	ConfigurableApplicationContext context = 
			new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
	
    	CustomerService cust = (CustomerService)context.getBean("customerService");
    	
    	System.out.println(cust);
    	
    	context.close();
    }
}

ConfigurableApplicationContext.close()将关闭应用程序上下文,释放所有资源并破坏所有缓存的单例bean。 仅用于destroy()方法演示目的🙂

输出量

Init method after properties are set : im property message
com.mkyong.customer.services.CustomerService@47393f
...
INFO: Destroying singletons in org.springframework.beans.factory.
support.DefaultListableBeanFactory@77158a: 
defining beans [customerService]; root of factory hierarchy
Spring Container is destroy! Customer clean up

设置message属性后,将调用afterPropertiesSet()方法。 而destroy()方法是在context.close()之后调用的;

想法...
我不建议使用InitializingBean和DisposableBean接口,因为它会将您的代码紧密耦合到Spring。 更好的方法应该是在bean配置文件中指定init-method和destroy-method属性。

下载源代码

下载它– Spring-InitializingBean-DisposableBean-Example.zip

参考文献

  1. 初始化Bean Javadoc
  2. 一次性Bean Javadoc

翻译自: https://mkyong.com/spring/spring-initializingbean-and-disposablebean-example/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值