在上面几章,描述了整个xml解析成BeanDefinition的过程。还剩余最后一个步骤就是向IOC中注册BeanDefinition。回到DefaultBeanDefinitionDocumentReader中,通过BeanDefinitionParserDelegate.parseBeanDefinitionElement()获得BeanDefinitionHolder,再会通过BeanDefinitionReaderUtils.registerBeanDefinition()将其注册。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public static void registerBeanDefinition( BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry) throws BeanDefinitionStoreException {
// 获取bean的名称 String beanName = definitionHolder.getBeanName() // 向IOC注册BeanDefinition registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition())
// 如果存在别名,则向IOC保存alias到name的映射关系 String[] aliases = definitionHolder.getAliases() if (aliases != null) { for (String alias : aliases) { registry.registerAlias(beanName, alias) } } }
|
保存beanName->beanDefinition的映射关系
DefaultListableBeanFactory使用的是HashMap保存BeanDefinition。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException {
Assert.hasText(beanName, "Bean name must not be empty"); Assert.notNull(beanDefinition, "BeanDefinition must not be null");
if (beanDefinition instanceof AbstractBeanDefinition) { try { ((AbstractBeanDefinition) beanDefinition).validate(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName, "Validation of bean definition failed", ex); } }
BeanDefinition oldBeanDefinition;
oldBeanDefinition = this.beanDefinitionMap.get(beanName); if (oldBeanDefinition != null) { if (!isAllowBeanDefinitionOverriding()) { throw new ... } else if (oldBeanDefinition.getRole() < beanDefinition.getRole()) { if (this.logger.isWarnEnabled()) { this.logger.warn(...); } } else if (!beanDefinition.equals(oldBeanDefinition)) { if (this.logger.isInfoEnabled()) { this.logger.info(....); } } else { if (this.logger.isDebugEnabled()) { this.logger.debug(...); } } this.beanDefinitionMap.put(beanName, beanDefinition); } else { if (hasBeanCreationStarted()) { synchronized (this.beanDefinitionMap) { this.beanDefinitionMap.put(beanName, beanDefinition); List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1); updatedDefinitions.addAll(this.beanDefinitionNames); updatedDefinitions.add(beanName); this.beanDefinitionNames = updatedDefinitions; if (this.manualSingletonNames.contains(beanName)) { Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames); updatedSingletons.remove(beanName); this.manualSingletonNames = updatedSingletons; } } } else { this.beanDefinitionMap.put(beanName, beanDefinition); this.beanDefinitionNames.add(beanName); this.manualSingletonNames.remove(beanName); } this.frozenBeanDefinitionNames = null; }
if (oldBeanDefinition != null || containsSingleton(beanName)) { resetBeanDefinition(beanName); } }
|
保存alias->beanName的映射关系
注册别名,在xml的IOC分支中,解析alias标签的时候就已经注册了,点击查看:解析 alias 标签
至此,BeanDefinition全部解析完成并交由IOC管理,才完成整个IOC的启动,后续IOC的bean的创建,依赖注入等所有的工作都依赖于此时的BeanDefinition。