spring复习:(35)在getBean时,在哪里根据普通bean和工厂bean进行区分处理来返回的?

在AbstractBeanFactory的doGetBean方法:
spring复习:(35)在getBean时,在哪里根据普通bean和工厂bean进行区分处理来返回的?_第1张图片
调用的getObjectForBeanInstance方法部分代码如下:
spring复习:(35)在getBean时,在哪里根据普通bean和工厂bean进行区分处理来返回的?_第2张图片
如果不是工厂bean,则直接将实例返回,否则调用getObjectFromFactoryBean方法获取工厂bean的getObject方法返回的对象

	protected Object getObjectFromFactoryBean(FactoryBean factory, String beanName, boolean shouldPostProcess) {
		if (factory.isSingleton() && containsSingleton(beanName)) {
			synchronized (getSingletonMutex()) {
				Object object = this.factoryBeanObjectCache.get(beanName);
				if (object == null) {
					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 {
						if (shouldPostProcess) {
							if (isSingletonCurrentlyInCreation(beanName)) {
								// Temporarily return non-post-processed object, not storing it yet..
								return object;
							}
							beforeSingletonCreation(beanName);
							try {
								object = postProcessObjectFromFactoryBean(object, beanName);
							}
							catch (Throwable ex) {
								throw new BeanCreationException(beanName,
										"Post-processing of FactoryBean's singleton object failed", ex);
							}
							finally {
								afterSingletonCreation(beanName);
							}
						}
						if (containsSingleton(beanName)) {
							this.factoryBeanObjectCache.put(beanName, object);
						}
					}
				}
				return object;
			}
		}
		else {
			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;
		}
	}

其中调用了doGetObjectFromFactoryBean方法,代码如下:
spring复习:(35)在getBean时,在哪里根据普通bean和工厂bean进行区分处理来返回的?_第3张图片
可以看到工厂bean的getObject方法被调用

你可能感兴趣的:(Spring,spring,java,后端)