十三、Spring源码学习之getBean方法-FactoryBeanRegistrySupport#getObjectFromFactoryBean方法

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;
		}
	}
上一篇:在idea中如何使用Git做开发


下一篇:HTML 怎么解决上下标问题呢?