publicstaticvoidregisterBeanDefinition(BeanDefinitionHolder definitionHolder,BeanDefinitionRegistry registry)throwsBeanDefinitionStoreException{// Register bean definition under primary name.String beanName = definitionHolder.getBeanName();
registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());// Register aliases for bean name, if any.String[] aliases = definitionHolder.getAliases();if(aliases !=null){for(String alias : aliases){
registry.registerAlias(beanName, alias);}}}
进入registerBeanDefinition()方法
@OverridepublicvoidregisterBeanDefinition(String beanName,BeanDefinition beanDefinition)throwsBeanDefinitionStoreException{Assert.hasText(beanName,"Bean name must not be empty");Assert.notNull(beanDefinition,"BeanDefinition must not be null");if(beanDefinition instanceofAbstractBeanDefinition){try{((AbstractBeanDefinition) beanDefinition).validate();}catch(BeanDefinitionValidationException ex){thrownewBeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,"Validation of bean definition failed", ex);}}//先判断BeanDefinition是否已经注册BeanDefinition existingDefinition =this.beanDefinitionMap.get(beanName);if(existingDefinition !=null){if(!isAllowBeanDefinitionOverriding()){thrownewBeanDefinitionOverrideException(beanName, beanDefinition, existingDefinition);}elseif(existingDefinition.getRole()< beanDefinition.getRole()){// e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTUREif(logger.isInfoEnabled()){
logger.info("Overriding user-defined bean definition for bean '"+ beanName +"' with a framework-generated bean definition: replacing ["+
existingDefinition +"] with ["+ beanDefinition +"]");}}elseif(!beanDefinition.equals(existingDefinition)){if(logger.isDebugEnabled()){
logger.debug("Overriding bean definition for bean '"+ beanName +"' with a different definition: replacing ["+ existingDefinition +"] with ["+ beanDefinition +"]");}}else{if(logger.isTraceEnabled()){
logger.trace("Overriding bean definition for bean '"+ beanName +"' with an equivalent definition: replacing ["+ existingDefinition +"] with ["+ beanDefinition +"]");}}this.beanDefinitionMap.put(beanName, beanDefinition);}else{if(hasBeanCreationStarted()){// Cannot modify startup-time collection elements anymore (for stable iteration)synchronized(this.beanDefinitionMap){this.beanDefinitionMap.put(beanName, beanDefinition);List<String> updatedDefinitions =newArrayList<>(this.beanDefinitionNames.size()+1);
updatedDefinitions.addAll(this.beanDefinitionNames);
updatedDefinitions.add(beanName);this.beanDefinitionNames = updatedDefinitions;removeManualSingletonName(beanName);}}else{//把beanDefinition缓存到map中// Still in startup registration phasethis.beanDefinitionMap.put(beanName, beanDefinition);//把beanName放到beanDefinitionNames list中,这个list着重记住,bean实例化的时候需要用到this.beanDefinitionNames.add(beanName);removeManualSingletonName(beanName);}this.frozenBeanDefinitionNames =null;}if(existingDefinition !=null||containsSingleton(beanName)){resetBeanDefinition(beanName);}elseif(isConfigurationFrozen()){clearByTypeCache();}}