依赖注入的功能,在初始化时完成。
Class->BeanDefinition->object的转换过程。
BeanDefinition和BeanFactoryPostProcessor
InvokeBeanFactoryPostProcessors 完成对对象的扫描
finishBeanFactoryInitialization spring开始实例化单例的类
三级缓存,在DefaultSingletonBeanRegistry中:
- singletonObjects: bean name to bean instance
- earlySingletonObjects: bean name to bean instance
- singletonFactory: bean name to ObjectFactory
ObjectFactory的定义:
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return (singletonObject != NULL_OBJECT ? singletonObject : null);
}
为什么需要三级缓存,两级缓存是否可以?