// Check with full lock now in order to enforce the same merged instance. // 尝试找出相同的实例名,并准备合并 if (containingBd == null) { mbd = this.mergedBeanDefinitions.get(beanName); }
// 并没有找到相同的实例 if (mbd == null) {
// parentName == null,说明当前已经是 RootBeanDefinition,那么就直接使用,clone(this)并重新赋值 if (bd.getParentName() == null) { // Use copy of given root bean definition. if (bd instanceof RootBeanDefinition) { mbd = ((RootBeanDefinition) bd).cloneBeanDefinition(); } else { mbd = new RootBeanDefinition(bd); } } else { // Child bean definition: needs to be merged with parent. // 如果存在 parentName, 那么说明当前 BeanDefinition 为 ChildBeanDefinition // 那么就继续调用 getMergedBeanDefinition(),递归获取 RootBeanDefinition,并不断的合并创建对象并属性拷贝 BeanDefinition pbd; try {
// 这里的 transFormedBeanName 是为了获取 bean 的真正名称 // 因为如果是 factoryBean 创建出来的对象,那么它的名称中将带有 &,而目的就是为了 remove it String parentBeanName = transformedBeanName(bd.getParentName()); if (!beanName.equals(parentBeanName)) { pbd = getMergedBeanDefinition(parentBeanName); } else { BeanFactory parent = getParentBeanFactory(); if (parent instanceof ConfigurableBeanFactory) { pbd = ((ConfigurableBeanFactory) parent).getMergedBeanDefinition(parentBeanName); } else { throw new NoSuchBeanDefinitionException(parentBeanName, "Parent name '" + parentBeanName + "' 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); // 覆盖旧属性 mbd.overrideFrom(bd); }
// Set default singleton scope, if not configured before. // 如果之前没有配置作用域,那么就设置默认的 singleton scope 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. if (containingBd != null && !containingBd.isSingleton() && mbd.isSingleton()) { mbd.setScope(containingBd.getScope()); }
// Cache the merged bean definition for the time being // (it might still get re-merged later on in order to pick up metadata changes) if (containingBd == null && isCacheBeanMetadata()) { this.mergedBeanDefinitions.put(beanName, mbd); } }