https://github.com/spring-projects/spring-framework/blob/9d768a89d22a1216613d6de38617aff625747c0b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java#L180-L203

I noticed that DefaultSingletonBeanRegistry#getSingleton(java.lang.String, boolean) lock singletonObjects,This leads me a question,Why it lock singletonObjects instead of the beanName? Is this to prevent bean cyclic dependencies in InitMethod? I can imagine a scenario, if multiple user threads directly use the bean factory exposed by BeanFactoryAware, ApplicationContextAware, while a lazy-loaded bean or runtime loaded bean is being loaded. The getBean of any other user thread will be blocked. If a lock on the map is not necessary, can it be replaced with a lock on the beanName String which in constant pool? Is there a friend who can answer my question? Thank you for answering

Comment From: jhoeller

The reason is that each singleton bean is typically a part of a whole graph of singleton beans. If different beans from different parts of that graph are requested by different threads and have yet to be created, there is a potential for deadlocks if each bean is created individually. As a consequence, we perform all such creation within a general singleton lock. Only when a bean has been made available already, it can be requested without a lock out of our ConcurrentHashMap of existing beans.