【Spring注解系列09】Spring初始化和销毁接口-InitializingBean与DisposableBean

本文介绍了Spring中InitializingBean和DisposableBean接口的用途。InitializingBean用于自定义初始化逻辑,确保属性设置正确;而DisposableBean接口则用于在bean销毁时释放资源。当ApplicationContext关闭时,所有单例bean的 DisposableBean 方法将被调用。
摘要由CSDN通过智能技术生成

1.InitializingBean与DisposableBean

InitializingBean

定义初始化逻辑,用于执行自定义初始化或者校验已设置的属性值等。

* Interface to be implemented by beans that need to react once all their properties
* have been set by a {@link BeanFactory}: e.g. to perform custom initialization,
* or merely to check that all mandatory properties have been set.

DisposableBean

定义销毁逻辑,用于一个实例bean销毁后需要释放资源等。 只有bean从容器中移除或者销毁或容器关闭时,才会调用该方法。

ApplicationContext关闭时,默认对所有单例bean都会调用这个方法。

* Interface to be implemented by beans that want to release resources on destruction.
* A {@link BeanFactory} will invoke the destroy method on individual destruction of a
* scoped bean. An {@link org.springframework.context.ApplicationContext} is supposed
* to dispose all of its singletons on shutdown, driven by the application lifecycle.

2.实例

//实现InitializingB
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值