Talk is cheap. Show me the code

第一步:定义一个Bean用来测试。
package com.xxx.hyl.ioc;
/**
* Bean 销毁演示
* @author 君战
*
* **/
public class DestroyBean {
}
第二步:使用IoC容器来销毁Bean,再次获取。
package com.xxx.hyl.ioc;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/****
* 演示Bean销毁后依然可以从IoC容器中获取
* @author 君战
* */
public class DestroyBeanDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(DestroyBean.class);
context.refresh();
DestroyBean beforeDestroy = context.getBean(DestroyBean.class);
System.out.println("执行Bean销毁前:" + beforeDestroy);
context.getAutowireCapableBeanFactory().destroyBean(beforeDestroy);
DestroyBean afterDestroy = context.getBean(DestroyBean.class);
System.out.println("执行Bean销毁后:" + afterDestroy);
}
}
第三步:查看控制台打印

需要搞明白的一点是,Bean所实现的或遵守的Spring提供的接口或者按照JSR-330规范的销毁方法被调用时,并不意味着该对象即将被GC掉,这和JVM的垃圾回收机制没有任何关系,也不意味它会被IoC容器给移除掉。
可以通过手动调用AutowireCapableBeanFactory提供的destroyBean方法来触发指定Bean所实现的销毁方法的调用。该方法需要传入Bean实例。
/**
* Destroy the given bean instance (typically coming from {@link #createBean}),
* applying the {@link org.springframework.beans.factory.DisposableBean} contract as well as
* registered {@link DestructionAwareBeanPostProcessor DestructionAwareBeanPostProcessors}.
*
Any exception that arises during destruction should be caught
* and logged instead of propagated to the caller of this method.
* @param existingBean the bean instance to destroy
*/
void destroyBean(Object existingBean);
天空丶泪
发表于 2020-12-31
public void destroyBean(Object existingBean) {
new DisposableBeanAdapter(existingBean, getBeanPostProcessors(), getAccessControlContext()).destroy();
}
// DisposableBeanAdapter#DisposableBeanAdapter(Object, List, AccessControlContext)
public DisposableBeanAdapter(Object bean, List postProcessors, AccessControlContext acc) {
Assert.notNull(bean, "Disposable bean must not be null");
this.bean = bean;
this.beanName = bean.getClass().getName();
this.invokeDisposableBean = (this.bean instanceof DisposableBean);
this.nonPublicAccessAllowed = true;
this.acc = acc;
this.beanPostProcessors = filterPostProcessors(postProcessors, bean);
}
评论列表
加载数据中...
klmk
发表于 2020-12-31
评论列表
加载数据中...
微梦
发表于 2020-12-31
评论列表
加载数据中...
1679644933
发表于 2020-12-31
// DisposableBeanAdapter#destroy
public void destroy() {
if (!CollectionUtils.isEmpty(this.beanPostProcessors)) {
for (DestructionAwareBeanPostProcessor processor : this.beanPostProcessors) {
processor.postProcessBeforeDestruction(this.bean, this.beanName);
}
}
if (this.invokeDisposableBean) {
if (logger.isTraceEnabled()) {
logger.trace("Invoking destroy() on bean with name '" + this.beanName + "'");
}
try {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedExceptionAction) () -> {
((DisposableBean) this.bean).destroy();
return null;
}, this.acc);
} else {
((DisposableBean) this.bean).destroy();
}
} catch (Throwable ex) {
String msg = "Invocation of destroy method failed on bean with name '" + this.beanName + "'";
if (logger.isDebugEnabled()) {
logger.warn(msg, ex);
} else {
logger.warn(msg + ": " + ex);
}
}
}
//在这里调用Bean自定义的销毁方法
if (this.destroyMethod != null) {
invokeCustomDestroyMethod(this.destroyMethod);
} else if (this.destroyMethodName != null) {
Method methodToInvoke = determineDestroyMethod(this.destroyMethodName);
if (methodToInvoke != null) {
invokeCustomDestroyMethod(ClassUtils.getInterfaceMethodIfPossible(methodToInvoke));
}
}
}
评论列表
加载数据中...
1008612
发表于 2020-12-31
以上就是我们对Bean被IoC容器销毁后还能使用吗问题的分析,答案是依然可以使用,但不建议使用,因为Bean可能在销毁方法中释放了一些资源或者修改了一些状态,使得Bean不再处于正确的状态
评论列表
加载数据中...
梦如南筏
发表于 2021-1-1
评论列表
加载数据中...
道创网站官方账号
发表于 2021-1-1
评论列表
加载数据中...
天宇
发表于 2021-1-1
评论列表
加载数据中...
2670459508
发表于 2021-1-1
评论列表
加载数据中...
明明哦
发表于 2021-1-1
评论列表
加载数据中...