/** Whether to allow re-registration of a different definition with the same name. */ privateboolean allowBeanDefinitionOverriding = true;
/** Map of bean definition objects, keyed by bean name. */ privatefinal Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256); /** List of bean definition names, in registration order. */ privatevolatile List<String> beanDefinitionNames = new ArrayList<>(256); /** List of names of manually registered singletons, in registration order. */ privatevolatile Set<String> manualSingletonNames = new LinkedHashSet<>(16); /** Cached array of bean definition names in case of frozen configuration. */ @Nullable privatevolatile String[] frozenBeanDefinitionNames;
// 校验 beanName 与 beanDefinition 非空 Assert.hasText(beanName, "Bean name must not be empty"); Assert.notNull(beanDefinition, "BeanDefinition must not be null");
/** Map from alias to canonical name. */ // key: alias // value: beanName privatefinal Map<String, String> aliasMap = new ConcurrentHashMap<>(16);
@Override publicvoidregisterAlias(String name, String alias){ // 校验 name 、 alias Assert.hasText(name, "'name' must not be empty"); Assert.hasText(alias, "'alias' must not be empty"); synchronized (this.aliasMap) { // name == alias 则去掉alias if (alias.equals(name)) { this.aliasMap.remove(alias); if (logger.isDebugEnabled()) { logger.debug("Alias definition '" + alias + "' ignored since it points to same name"); } } else { // 获取 alias 已注册的 beanName String registeredName = this.aliasMap.get(alias); // 已存在 if (registeredName != null) { // 相同,则 return ,无需重复注册 if (registeredName.equals(name)) { // An existing alias - no need to re-register return; } // 不允许覆盖,则抛出 IllegalStateException 异常 if (!allowAliasOverriding()) { thrownew IllegalStateException("Cannot define alias '" + alias + "' for name '" + name + "': It is already registered for name '" + registeredName + "'."); } if (logger.isDebugEnabled()) { logger.debug("Overriding alias '" + alias + "' definition for registered name '" + registeredName + "' with new target name '" + name + "'"); } } // 校验,是否存在循环指向 checkForAliasCircle(name, alias); // 注册 alias this.aliasMap.put(alias, name); if (logger.isTraceEnabled()) { logger.trace("Alias definition '" + alias + "' registered for name '" + name + "'"); } } } }
注册 alias 和注册 BeanDefinition 的过程差不多。
在最后,调用了 #checkForAliasCircle() 来对别名进行了循环检测。代码如下:
protectedvoidcheckForAliasCircle(String name, String alias){ if (hasAlias(alias, name)) { thrownew IllegalStateException("Cannot register alias '" + alias + "' for name '" + name + "': Circular reference - '" + name + "' is a direct or indirect alias for '" + alias + "' already"); } } publicbooleanhasAlias(String name, String alias){ for (Map.Entry<String, String> entry : this.aliasMap.entrySet()) { String registeredName = entry.getValue(); if (registeredName.equals(name)) { String registeredAlias = entry.getKey(); if (registeredAlias.equals(alias) || hasAlias(registeredAlias, alias)) { returntrue; } } } returnfalse; }