简体   繁体   中英

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. 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. 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. All are singletons, B is injected in A and C is injected in B.

I get BeanFactoryReference , get these beans and then release BeanFactoryReference .

I then again get BeanFactoryReference ; beans A and B are new but C is from old context (I printed their system hash codes to check it).

This isn't an issue in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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