getBean
所有的getBean方法最终底层都会调用doGetBean方法
protected <T> T doGetBean(final String name,final Class<T> requiredType,
final Object[] args, boolean typeCheckOnly) throws BeansException
transformedBeanName
去掉beanName所有的&前缀,通过aliasMap获取到最终的beanName
getSingleton 获取提前注册的单例bean🙈
protected Object getSingleton(String beanName, boolean allowEarlyReference){
// 先在缓存中获取
Object singletonObject = this.singletonObjects.get(beanName);
// 缓存中不存在这个bean并且这个bean正在创建中
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
// 从提前实例化的bean中获取
singletonObject = this.earlySingletonObjects.get(beanName);
// bean不存在并且运行提前引用未实例化完成的bean
if (singletonObject == null && allowEarlyReference) {
// 从提前暴露出来的bean中获取引用
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
// 实例化提前暴露的bean🙈
singletonObject = singletonFactory.getObject();
// 将实例化的结果放入提前实例化的earlySingletonObjects中
this.earlySingletonObjects.put(beanName, singletonObject);
// 从提前暴露出来的bean引用中去掉
this.singletonFactories.remove(beanName);
}
}
}
}
return singletonObject;
}
缓存中未获取到bean:
原型bean判断是否有有循环依赖(当前需要获取的bean正在创建中)
如果存在父beanFactory容器,从父容器中获取
typeCheckOnly为false,标记当前bean为已创建,加入到alreadyCreated中,并从mergedBeanDefinitions从清除该bean
获取当前beanDefinition,获取依赖列表,并维护到dependentBeanMap和dependenciesForBeanMap中,实例化依赖的bean
单例bean:
getSingleton()
singletonObjects中获取
beforeSingletonCreation:
加入正在创建列表singletonsCurrentlyInCreation中
ObjectFactory创建bean
createBean:
<重点重点重重点>
afterSingletonCreation:
从正在创建列表singletonsCurrentlyInCreation中删除
addSingleton:维护实例化好的bean实例
protected void addSingleton(String beanName, Object singletonObject) {
synchronized (this.singletonObjects) {
this.singletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
this.earlySingletonObjects.remove(beanName);
this.registeredSingletons.add(beanName);
}
}
原型bean:
beforePrototypeCreation
加入正在创建列表prototypesCurrentlyInCreation
createBean:
<重点重点重重点>
afterPrototypeCreation
从正在创建列表prototypesCurrentlyInCreation中删除
其他scope:🙈
根据scope封装不同的获取策略
createBean
resolveBeanClass解析beanClass
BeanPostProcessor=》InstantiationAwareBeanPostProcessor
实例化前的扩展点:postProcessBeforeInstantiation、postProcessAfterInitialization🚩
doCreateBean:
createBeanInstance创建bean实例
instantiateUsingFactoryMethod factory-method
autowireConstructor 构造器 SmartInstantiationAwareBeanPostProcessor#determineCandidateConstructors
instantiateBean 实例化bean
默认构造函数构造
MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition
是否运行提前暴露:earlySingletonExposure
protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(singletonFactory, "Singleton factory must not be null");
synchronized (this.singletonObjects) {
if (!this.singletonObjects.containsKey(beanName)) {
// 在singletonFactories中暴露
this.singletonFactories.put(beanName, singletonFactory);
this.earlySingletonObjects.remove(beanName);
// 在registeredSingletons中暴露
this.registeredSingletons.add(beanName);
}
}
}
初始化bean实例
InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation🚩
populateBean 处理IOC注入逻辑autowireByName,autowireByType
InstantiationAwareBeanPostProcessor#postProcessPropertyValues🚩
初始化 initializeBean
invokeAwareMethods 处理BeanNameAware/BeanClassLoaderAware/BeanFactoryAware🚩
BeanPostProcessor#postProcessBeforeInitialization🚩
InitializingBean#afterPropertiesSet🚩
initMethodName执行🚩
BeanPostProcessor#postProcessAfterInitialization🚩
getObjectForBeanInstance
beanName以&开头,实例不是FactoryBean实现类,报错
实例不是FactoryBean实现类或beanName以&开头,返回实例
beanName不以&开头并且实例是FactoryBean实现类
传入beanDefinition为空,尝试从factoryBeanObjectCache缓存中获取
getObjectFromFactoryBean
factory.getObject()
非合成的bean,BeanPostProcessor处理
postProcessObjectFromFactoryBean=》beanProcessor.postProcessAfterInitialization
变量:
Set<String> singletonsCurrentlyInCreation
Map<String, ObjectFactory<?>> singletonFactories
Map<String, Object> earlySingletonObjects
Map<String, Object> factoryBeanObjectCache
Map<String, BeanDefinition> beanDefinitionMap
Map<String, RootBeanDefinition> mergedBeanDefinitions
ThreadLocal<Object> prototypesCurrentlyInCreation
Set<String> alreadyCreated
Map<String, Set<String>> dependentBeanMap
Map<String, Set<String>> dependenciesForBeanMap
网友评论