我已经逐渐掌握了Spring一段时间,并且认为我对这些概念有一个合理的想法,但是我在另一个线程中遇到了信息,这让我的事情变得颠倒了……
“…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. 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.”
这让我觉得我有真正的用例,我想使用原型bean,例如,我需要每个请求一个“新”bean实例.但是根据我对这个片段的理解(来自Spring 3文档),Spring坚持引用需要清理的bean(引用本身意味着bean不会被垃圾收集器自动清除).此外,我认为必须手动清理原型bean所拥有的资源.
如果这是正确的,有人可以告诉我吗?如果是这样,有一个典型的模式来处理这个?我很欣赏一个答案,它可以描述Spring以这种方式实现原型bean的架构原因.