1.4 KiB
1.4 KiB
顺序获取单例
- private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
- private final Map<String, Object> earlySingletonObjects = new ConcurrentHashMap<>(16);
- private final Map<String, ObjectFactory> singletonFactories = new HashMap<>(16);
循环依赖
// 单例顺序获取失败
// Fail if we're already creating this bean instance:
// We're assumably within a circular reference.
- 判断是否存在父类中,如果存在从父类中获取。
- 设置正在创建
- RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
- 判断单例、原型或者其他
- 创建bean,检验检测状态
- Make sure bean class is actually resolved at this point, and clone the bean definition in case of a dynamically resolved Class which cannot be stored in the shared merged bean definition.
- doCreateBean
- createBeanInstance
- Eagerly cache singletons to be able to resolve circular references even when triggered by lifecycle interfaces like BeanFactoryAware.放入三级缓存中addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean))
- Give any InstantiationAwareBeanPostProcessors the opportunity to modify the state of the bean before properties are set. This can be used, for example,to support styles of field injection.
重要类
BeanDefinition、ClassLoader