I asked the question on Stackoverflow. https://stackoverflow.com/questions/77731162/why-spring-need-remove-earlysingletonobjects

I know I shouldn't ask questions here. But on Stackoverflow no one to answer my question. I hope your can help me.

I don't know why addSingletonFactory() need call this.earlySingletonObjects.remove(beanName)?

What will happen if this method this.earlySingletonObjects.remove(beanName) is not called?

Can you show me some examples? @jhoeller I see that you are the author of DefaultSingletonBeanRegistry.

I hope you've enlightened me. Thank you so much!

Comment From: jhoeller

That's just enforcing clean state within the registry. In all regular scenarios, no earlySingletonObjects entry will exist at the point of such an addSingletonFactory call. Just if there was a previous attempt at creating a singleton object for the same bean name, with the new addSingletonFactory call happening before the old bean was fully created, there could theoretically be a leftover entry in earlySingletonObjects. For 100% consistency, we make sure that such an entry is being removed before the new singleton factory goes into action. In practical scenarios, this is usually not necessary but does not hurt either.