我注意到我的原型(prototype)作用域 Spring bean 的 @PreDestroy
钩子(Hook)没有被执行。
我已经阅读了here这实际上是设计使然。 Spring 容器将销毁单例 bean,但不会销毁原型(prototype) bean。我不清楚为什么。如果 Spring 容器将创建我的原型(prototype) bean 并执行它的 @PostConstruct
钩子(Hook),为什么当容器关闭时它不会破坏我的 bean?一旦我的 Spring 容器关闭,继续使用它的任何 bean 是否有意义?我看不到您想要在完成其 bean 之前关闭容器的场景。在容器关闭后是否可以继续使用原型(prototype) Spring bean?
上面描述了我的主要问题的令人费解的背景,即:如果 Spring 容器没有破坏原型(prototype) bean,这是否意味着可能发生内存泄漏?或者原型(prototype) bean 会在某个时候被垃圾回收吗?
Spring 文档指出:
The client code must clean up prototype-scoped objects and release expensive resources that the prototype bean(s) are holding. To get the Spring container to release resources held by prototype-scoped beans, try using a custom bean post-processor, which holds a reference to beans that need to be cleaned up.
这是什么意思?文本向我表明,作为程序员,我有责任明确(手动)销毁我的原型(prototype) bean。这个对吗?如果是这样,我该怎么做?
最佳答案
为了他人的利益,我将在下面介绍我从调查中收集到的内容:
只要原型(prototype) bean 本身不持有对另一个资源(例如数据库连接或 session 对象)的引用,只要对该对象的所有引用都被删除或对象退出,它就会被垃圾回收范围。因此通常不需要显式销毁原型(prototype) bean。
但是,在如上所述可能发生内存泄漏的情况下,可以通过创建一个单例 bean 后处理器来销毁原型(prototype) bean,该后处理器的销毁方法显式调用原型(prototype) bean 的销毁钩子(Hook)。因为后处理器本身是单例范围的,所以它的销毁钩子(Hook)将被 Spring 调用:
创建一个 bean 后处理器来处理所有原型(prototype) bean 的销毁。这是必要的,因为 Spring 不会破坏原型(prototype) bean,因此代码中的任何 @PreDestroy 钩子(Hook)都不会被容器调用。
实现以下接口(interface):
1.BeanFactoryAware
该接口(interface)提供了一个回调方法,该方法接收一个 Beanfactory 对象。这个 BeanFactory 对象在后处理器类中用于通过其 BeanFactory.isPrototype(String beanName) 方法识别所有原型(prototype) bean。
2. DisposableBean
该接口(interface)提供了一个由 Spring 容器调用的 Destroy() 回调方法。我们将从该方法中调用所有原型(prototype) bean 的 Destroy() 方法。
3. BeanPostProcessor
实现这个接口(interface)提供了对后处理回调的访问,我们在其中准备了一个由 Spring 容器实例化的所有原型(prototype)对象的内部 List<>。稍后我们将遍历这个 List<> 来销毁我们的每个原型(prototype) bean。
3. 最后在你的每个原型(prototype) bean 中实现 DisposableBean 接口(interface),提供本合约所需的 Destroy() 方法。
为了说明这个逻辑,我在下面提供了一些取自 article 的代码。 :
/**
* Bean PostProcessor that handles destruction of prototype beans
*/
@Component
public class DestroyPrototypeBeansPostProcessor implements BeanPostProcessor, BeanFactoryAware, DisposableBean {
private BeanFactory beanFactory;
private final List<Object> prototypeBeans = new LinkedList<>();
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (beanFactory.isPrototype(beanName)) {
synchronized (prototypeBeans) {
prototypeBeans.add(bean);
}
}
return bean;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
@Override
public void destroy() throws Exception {
synchronized (prototypeBeans) {
for (Object bean : prototypeBeans) {
if (bean instanceof DisposableBean) {
DisposableBean disposable = (DisposableBean)bean;
try {
disposable.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
prototypeBeans.clear();
}
}
}
关于java - Spring原型(prototype)bean是否需要手动销毁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50681027/