这里直接上代码,进行相关分析关于getBean方法的整体解析可以看这里getBean方法解析
//name:getBean方法传入的name,beanName:处理过后的name,mbd:null
protected Object getObjectForBeanInstance(
Object beanInstance, String name, String beanName, RootBeanDefinition mbd) {
// Don't let calling code try to dereference the factory if the bean isn't a factory.
//如果要获取的bean是FactoryBean的引用,并且缓存的对象不是FactoryBean类型。意思就是要获取的bean实现了FactoryBean(属于工厂),但是获取的实例又不是FactoryBean类型的抛错。bean是否是工厂是由它是否实现FactoryBean接口决定的
if (BeanFactoryUtils.isFactoryDereference(name) && !(beanInstance instanceof FactoryBean)) {
throw new BeanIsNotAFactoryException(transformedBeanName(name), beanInstance.getClass());
}
// Now we have the bean instance, which may be a normal bean or a FactoryBean.If it's a FactoryBean, we use it to create a bean instance, unless the caller actually wants a reference to the factory.
//如果创建实例不是FactoryBean实例,或者是factoryBean的引用
if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {
return beanInstance;
}
Object object = null;
//根据处理过的beanName获取FactoryBean,上面方法传进来的mbd为null
if (mbd == null) {
object = getCachedObjectForFactoryBean(beanName);
}
//如果获取到的FactoryBean为空,说明这个bean没有实现FactoryBean接口
if (object == null) {
// Return bean instance from factory.
FactoryBean> factory = (FactoryBean>) beanInstance;
// Caches object obtained from FactoryBean if it is a singleton.
//如果bean已经加载过,则获取合并过后的RootBeanDefinition
if (mbd == null && containsBeanDefinition(beanName)) {
//-----------------------------------------合并父类和子类bean------------------------------------------------------//
mbd = getMergedLocalBeanDefinition(beanName);
//-----------------------------------------------------------------------------------------------//
}
boolean synthetic = (mbd != null && mbd.isSynthetic());
object = getObjectFromFactoryBean(factory, beanName, !synthetic);
}
return object;
}
//合并父类和子类bean 的方法
//第一步
protected RootBeanDefinition getMergedLocalBeanDefinition(String beanName) throws BeansException {
// Quick check on the concurrent map first, with minimal locking.
//根据beanName检查是否存在已经合并过的bean
RootBeanDefinition mbd = this.mergedBeanDefinitions.get(beanName);
if (mbd != null) {
return mbd;
}
return getMergedBeanDefinition(beanName, getBeanDefinition(beanName));
}
//第二步
protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd)
throws BeanDefinitionStoreException {
return getMergedBeanDefinition(beanName, bd, null);
}
//第三步
//containingBd null
protected RootBeanDefinition getMergedBeanDefinition(
String beanName, BeanDefinition bd, BeanDefinition containingBd)
throws BeanDefinitionStoreException {
synchronized (this.mergedBeanDefinitions) {
RootBeanDefinition mbd = null;
// Check with full lock now in order to enforce the same merged instance.
//通过beanName检查是否已经存在合并过的BeanDefinition
if (containingBd == null) {
mbd = this.mergedBeanDefinitions.get(beanName);
}
if (mbd == null) {
//查看是否存在父bean
if (bd.getParentName() == null) {
// Use copy of given root bean definition.
//如果不存在父bean,并且是RootBeanDefinition类型的bean,就复制一份RootBeanDefinition并设置给mbd
if (bd instanceof RootBeanDefinition) {
mbd = ((RootBeanDefinition) bd).cloneBeanDefinition();
}
//如果不是就把bd中的值复制mbd
else {
mbd = new RootBeanDefinition(bd);
}
}
else {
// Child bean definition: needs to be merged with parent.
//如果这个bean是一个子bean,就需要跟父bean进行合并
BeanDefinition pbd;
try {
//处理父bean的beanName
String parentBeanName = transformedBeanName(bd.getParentName());
//如果要过去的bean的beanName于父类的不一样,就行合并
if (!beanName.equals(parentBeanName)) {
//此方法最终调用的还是getMergedBeanDefinition方法
pbd = getMergedBeanDefinition(parentBeanName);
}
else {
if (getParentBeanFactory() instanceof ConfigurableBeanFactory) {
pbd = ((ConfigurableBeanFactory) getParentBeanFactory()).getMergedBeanDefinition(parentBeanName);
}
else {
throw new NoSuchBeanDefinitionException(bd.getParentName(),
"Parent name '" + bd.getParentName() + "' is equal to bean name '" + beanName +
"': cannot be resolved without an AbstractBeanFactory parent");
}
}
}
catch (NoSuchBeanDefinitionException ex) {
throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName,
"Could not resolve parent bean definition '" + bd.getParentName() + "'", ex);
}
// Deep copy with overridden values.
//复制合并之后的属性到要返回的RootBeanDefinition中
mbd = new RootBeanDefinition(pbd);
//将传入的BeanDefinition的属性复制一遍到新的RootBeanDefinition中
mbd.overrideFrom(bd);
}
// Set default singleton scope, if not configured before.
//设置是否单利的
if (!StringUtils.hasLength(mbd.getScope())) {
mbd.setScope(RootBeanDefinition.SCOPE_SINGLETON);
}
// A bean contained in a non-singleton bean cannot be a singleton itself.
// Let's correct this on the fly here, since this might be the result of
// parent-child merging for the outer bean, in which case the original inner bean
// definition will not have inherited the merged outer bean's singleton status.
//如果外部的bean不是单利的,但是合并之后的bean是单利的那么就将合并之后的按照外部的bean的单利情况来设定
if (containingBd != null && !containingBd.isSingleton() && mbd.isSingleton()) {
mbd.setScope(containingBd.getScope());
}
// Only cache the merged bean definition if we're already about to create an
// instance of the bean, or at least have already created an instance before.
//保存到缓存中
if (containingBd == null && isCacheBeanMetadata() && isBeanEligibleForMetadataCaching(beanName)) {
this.mergedBeanDefinitions.put(beanName, mbd);
}
}
return mbd;
}
}