gpt4 book ai didi

spring - 未在注册为原型(prototype)的 bean 上调用 destroy-method

转载 作者:行者123 更新时间:2023-12-02 06:38:07 25 4
gpt4 key购买 nike

我有一个注册为单例的 bean,并在其上定义了 init-method 和 destroy-method。我正在访问该 bean,并且可以看到对这两种方法的调用。但是,在将范围更改为原型(prototype)时,不会调用销毁方法。

我不明白这背后的理由。

最佳答案

阅读the documentation :

In contrast to the other scopes, Spring does not manage the complete lifecycle of a prototype bean: the container instantiates, configures, and otherwise assembles a prototype object, and hands it to the client, with no further record of that prototype instance. Thus, although initialization lifecycle callback methods are called on all objects regardless of scope, in the case of prototypes, configured destruction lifecycle callbacks are not called.

这是非常合乎逻辑的:您的应用程序可以每 10 毫秒请求一次原型(prototype) bean 的新实例,对 bean 执行某些操作,然后让它超出范围。如果 Spring 在应用程序关闭时必须 destroy() 它们,则它必须保留对每个创建的原型(prototype) bean 的引用,以防止它们被垃圾收集,从而导致内存泄漏。

关于spring - 未在注册为原型(prototype)的 bean 上调用 destroy-method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23442512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com