简体   繁体   English

Spring SingletonBeanFactoryLocator bean销毁

[英]Spring SingletonBeanFactoryLocator bean destruction

While using SingletonBeanFactoryLocator and its BeanFactoryReference I found out that when I call release on BeanFactoryReference instance, some of the beans in the context are destroyed but some continue to live. 在使用SingletonBeanFactoryLocator及其BeanFactoryReference我发现当我在BeanFactoryReference实例上调用release时,上下文中的一些bean被销毁但有些仍继续存在。 My assumption was that all of them would be destroyed. 我的假设是所有这些都会被摧毁。

A consequence of this is, next time I use SingletonBeanFactoryLocator and BeanFactoryReference with same keys, instead of getting brand new beans, I get some beans carried on from the previous context. 这样做的结果是,下次我使用具有相同键的SingletonBeanFactoryLocatorBeanFactoryReference ,而不是获取全新的bean时,我从前面的上下文中获取了一些bean。 I'm not able to find a pattern behind this. 我无法找到这背后的模式。 These beans are singletons yet some of them are created anew and some of them carry on from old context. 这些豆子是单身,但其中一些是重新创造的,其中一些是从旧的环境中继续进行的。 Why is it so? 为什么会这样?

Example: let's say I have beans A, B and C in my context. 示例:假设我在上下文中有bean A,B和C. All are singletons, B is injected in A and C is injected in B. 所有都是单例,B注入A,C注入B.

I get BeanFactoryReference , get these beans and then release BeanFactoryReference . 我得到BeanFactoryReference ,获取这些bean然后释放BeanFactoryReference

I then again get BeanFactoryReference ; 然后我再次获得BeanFactoryReference ; beans A and B are new but C is from old context (I printed their system hash codes to check it). bean A和B是新的,但C来自旧的上下文(我打印了他们的系统哈希码来检查它)。

This isn't an issue in Spring. 这在Spring中不是问题。 It does clear all the beans. 它确实清除了所有豆类。 There was a glitch in the code using this which made it appear as though some beans were not destroyed but reused. 使用这个代码时出现了一个小故障,这使得它看起来好像有些bean没有被销毁但是被重用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM