project/spring/spring源码解读/spring03BeanFactory.md

39 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 顺序获取单例
1. private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
2. private final Map<String, Object> earlySingletonObjects = new ConcurrentHashMap<>(16);
3. private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);
## 循环依赖
```java
// 单例顺序获取失败
// Fail if we're already creating this bean instance:
// We're assumably within a circular reference.
```
1. 判断是否存在父类中,如果存在从父类中获取。
2. 设置正在创建
3. RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
4. 判断单例、原型或者其他
5. 创建bean检验检测状态
1. 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.
2. doCreateBean
1. createBeanInstance
2. Eagerly cache singletons to be able to resolve circular references even when triggered by lifecycle interfaces like BeanFactoryAware.放入三级缓存中addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean))
3. 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.
## 重要类
```java
BeanDefinitionClassLoader
```