protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {
//判断FactoryBean 是单例的,并且一级缓存中不包含该bean
if (factory.isSingleton() && containsSingleton(beanName)) {
synchronized (getSingletonMutex()) {
//从FactoryBean缓存中获取
Object object = this.factoryBeanObjectCache.get(beanName);
if (object == null) {
//如果不存在,则调用FactoryBean中的getObject方法
object = doGetObjectFromFactoryBean(factory, beanName);
// Only post-process and store if not put there already during getObject() call above
// (e.g. because of circular reference processing triggered by custom getBean calls)
Object alreadyThere = this.factoryBeanObjectCache.get(beanName);
if (alreadyThere != null) {//这里再次获取 如果已经存在,则将之前的替换现在的
object = alreadyThere;
}
else {
//是否允许调用bean的后置处理器
if (shouldPostProcess) {
//如果该bean正在创建中,则直接返回
if (isSingletonCurrentlyInCreation(beanName)) {
// Temporarily return non-post-processed object, not storing it yet..
return object;
}
//将当前beanName加入到singletonsCurrentlyInCreation中,表示当前bean正在创建
beforeSingletonCreation(beanName);
try {
//调用bean后置处理器
object = postProcessObjectFromFactoryBean(object, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(beanName,
"Post-processing of FactoryBean's singleton object failed", ex);
}
finally {
//将当前beanName从singletonsCurrentlyInCreation中移除,表示当前bean已经创建完成
afterSingletonCreation(beanName);
}
}
if (containsSingleton(beanName)) {//放入缓存
this.factoryBeanObjectCache.put(beanName, object);
}
}
}
return object;
}
}
else {
//如果不是单例的,则直接调用FactoryBean的getObject方法
Object object = doGetObjectFromFactoryBean(factory, beanName);
if (shouldPostProcess) {
try {
object = postProcessObjectFromFactoryBean(object, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "Post-processing of FactoryBean's object failed", ex);
}
}
return object;
}
}
相关文章
- 04-18十三、Spring源码学习之getBean方法-FactoryBeanRegistrySupport#getObjectFromFactoryBean方法
- 04-18学习Spring-Data-Jpa(九)---注解式方法查询之@NamedQuery、@NamedNativeQuery
- 04-18Spring之BeanFactory:解析getBean()方法
- 04-18Spring之BeanFactory:解析getBean()方法
- 04-18springMvc源码学习之:spirngMvc获取请求参数的方法
- 04-18springMvc源码学习之:spirngMVC获取请求参数的方法2
- 04-18Spring 的 getBean 方法源码解析
- 04-18spring-boot-2.0.3启动源码篇二 - run方法(一)之SpringApplicationRunListener
- 04-18spring-boot-2.0.3启动源码篇四 - run方法(三)之createApplicationContext
- 04-18Android源码学习之模板方法模式应用