Spring 水滴石穿(二) 工厂实现之别名注册AliasRegistry

AliasRegistry接口

Ioc容器应该提供一种机制来灵活匹配bean实例,spring就提供了别名机制来处理多个name对应同一个bean的情况,或许有的同学会问那用一个map来存储这种情况不就好了干嘛多此一举要弄个别名机制呢?我的思考是一方面在删除一个bean时,需要遍历这个map找到对应的key来删除不够简单,性能也差,另一方面也会导致beanMap过于臃肿,不够简单明了,所以才有了AliasRegistry提供别名注册功能,正如代码

    //注册
    void registerAlias(String name, String alias);
    //移除
    void removeAlias(String alias);
    //是否时别名
    boolean isAlias(String name);
    //获得所有别名
    String[] getAliases(String name);

SimpleAliasRegistry实现

ublic class SimpleAliasRegistry implements AliasRegistry {

    /** 采用map存储别名映射关系 */
    private final Map aliasMap = new ConcurrentHashMap<>(16);


    @Override
    public void registerAlias(String name, String alias) {
        //注意ConcurrentHashMap只是单个内部操作安全,这里有复合操作,所以上锁
        synchronized (this.aliasMap) {
            if (alias.equals(name)) {
                this.aliasMap.remove(alias);
            }
            else {
                String registeredName = this.aliasMap.get(alias);
                if (registeredName != null) {
                    if (registeredName.equals(name)) {
                        // An existing alias - no need to re-register
                        return;
                    }
                    if (!allowAliasOverriding()) {
                        throw new IllegalStateException("Cannot define alias '" + alias + "' for name '" +
                                name + "': It is already registered for name '" + registeredName + "'.");
                    }
                }
                //检测循环引用,如果是的话抛出异常
                checkForAliasCircle(name, alias);
                this.aliasMap.put(alias, name);
            }
        }
    }

    //决定是否可以重写别名
    protected boolean allowAliasOverriding() {
        return true;
    }

    //校验是否指定beanName有指定的别名,别名可能是个引用链
    public boolean hasAlias(String name, String alias) {
        String registeredName = this.aliasMap.get(alias);
        return ObjectUtils.nullSafeEquals(registeredName, name) || (registeredName != null
                && hasAlias(name, registeredName));
    }

    @Override
    public void removeAlias(String alias) {
        synchronized (this.aliasMap) {
            String name = this.aliasMap.remove(alias);
            if (name == null) {
                throw new IllegalStateException("No alias '" + alias + "' registered");
            }
        }
    }

    @Override
    public boolean isAlias(String name) {
        return this.aliasMap.containsKey(name);
    }

    @Override
    public String[] getAliases(String name) {
        List result = new ArrayList<>();
        synchronized (this.aliasMap) {
            retrieveAliases(name, result);
        }
        return StringUtils.toStringArray(result);
    }

    //递归查找指定name的所有alias
    private void retrieveAliases(String name, List result) {
        this.aliasMap.forEach((alias, registeredName) -> {
            if (registeredName.equals(name)) {
                result.add(alias);
                retrieveAliases(alias, result);
            }
        });
    }

    //用指定的解析器解析所有的alias和name信息,暂不关注
    public void resolveAliases(StringValueResolver valueResolver) {
        synchronized (this.aliasMap) {
            Map aliasCopy = new HashMap<>(this.aliasMap);
            aliasCopy.forEach((alias, registeredName) -> {
                String resolvedAlias = valueResolver.resolveStringValue(alias);
                String resolvedName = valueResolver.resolveStringValue(registeredName);
                if (resolvedAlias == null || resolvedName == null || resolvedAlias.equals(resolvedName)) {
                    this.aliasMap.remove(alias);
                }
                else if (!resolvedAlias.equals(alias)) {
                    String existingName = this.aliasMap.get(resolvedAlias);
                    if (existingName != null) {
                        if (existingName.equals(resolvedName)) {
                            // Pointing to existing alias - just remove placeholder
                            this.aliasMap.remove(alias);
                            return;
                        }
                        throw new IllegalStateException(
                                "Cannot register resolved alias '" + resolvedAlias + "' (original: '" + alias +
                                "') for name '" + resolvedName + "': It is already registered for name '" +
                                registeredName + "'.");
                    }
                    checkForAliasCircle(resolvedName, resolvedAlias);
                    this.aliasMap.remove(alias);
                    this.aliasMap.put(resolvedAlias, resolvedName);
                }
                else if (!registeredName.equals(resolvedName)) {
                    this.aliasMap.put(alias, resolvedName);
                }
            });
        }
    }

    //检测循环引用的情况
    protected void checkForAliasCircle(String name, String alias) {
        if (hasAlias(alias, name)) {
            throw new IllegalStateException("Cannot register alias '" + alias +
                    "' for name '" + name + "': Circular reference - '" +
                    name + "' is a direct or indirect alias for '" + alias + "' already");
        }
    }
    
    //解析别名到真正的bean name
    public String canonicalName(String name) {
        String canonicalName = name;
        // Handle aliasing...
        String resolvedName;
        do {
            resolvedName = this.aliasMap.get(canonicalName);
            if (resolvedName != null) {
                canonicalName = resolvedName;
            }
        }
        while (resolvedName != null);
        return canonicalName;
    }

这里有几个注意点
1.为何需要循环引用的检测?

设想map中是这样子的A->B B->C C->A,假如你要获取A的真正的bean的name,是否就进入了死循环了?我们必须明白的一点是这个map中既可以存别名与实名的映射,也可以存别名与别名的映射,一个实名的所有别名在这个map中是个引用链。
2.循环引用检测的实现
还是上面的map,就不该存放C->A,它的实现就是要检测C->A是否循环,只需要检测A->C是否存在,又有可能存在间接的A->C引用,所以采用递归方式
3.获取别名的实名
前面说了这个map其实存放的是个引用链,所以要获取真正的实名,还是需要做个循环,从传参一直遍历到这个引用链的尾部去,这也就是canonicalName方法做的事情。

你可能感兴趣的:(java,spring)