SimpleAliasRegistry实现了AliasRegistry接口,用于别名的一些操作,下面是他的几个方法
首先是registerAlias方法
public void registerAlias(String name, String alias) {
Assert.hasText(name, "'name' must not be empty");
Assert.hasText(alias, "'alias' must not be empty");
if (alias.equals(name)) {
this.aliasMap.remove(alias);
}
else {
if (!allowAliasOverriding()) {
String registeredName = (String) this.aliasMap.get(alias);
if (registeredName != null && !registeredName.equals(name)) {
throw new IllegalStateException("Cannot register alias '" + alias + "' for name '" +
name + "': It is already registered for name '" + registeredName + "'.");
}
}
this.aliasMap.put(alias, name);
}
}
从上面的代码可以看出,首先先判断名字和别名是否相等,如果相等就从aliasMap中移除。
在不相等的情况下,用allowAliasOverriding()方法判断是否可以覆盖
下面是removeAlias方法
public void removeAlias(String alias) {
String name = (String) this.aliasMap.remove(alias);
if (name == null) {
throw new IllegalStateException("No alias '" + alias + "' registered");
}
}
从上面的代码可以看出,如果aliasMap中不存在alias这个key,则会抛出异常
下面是isAlias方法
public boolean isAlias(String name) {
return this.aliasMap.containsKey(name);
}
这个也很简单,判断aliasMap中是否有这个别名
getAliases方法
public String[] getAliases(String name) {
List aliases = new ArrayList();
synchronized (this.aliasMap) {
for (Iterator it = this.aliasMap.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String registeredName = (String) entry.getValue();
if (registeredName.equals(name)) {
aliases.add(entry.getKey());
}
}
}
return StringUtils.toStringArray(aliases);
}
这个方法是通过名称取得所有别名,注意synchronized,保证线程安全
resolveAliases方法
public void resolveAliases(StringValueResolver valueResolver) {
Assert.notNull(valueResolver, "StringValueResolver must not be null");
synchronized (this.aliasMap) {
Map aliasCopy = new HashMap(this.aliasMap);
for (Iterator it = aliasCopy.keySet().iterator(); it.hasNext();) {
String alias = (String) it.next();
String registeredName = (String) aliasCopy.get(alias);
String resolvedAlias = valueResolver.resolveStringValue(alias);
String resolvedName = valueResolver.resolveStringValue(registeredName);
if (!resolvedAlias.equals(alias)) {
String existingName = (String) this.aliasMap.get(resolvedAlias);
if (existingName != null && !existingName.equals(resolvedName)) {
throw new IllegalStateException("Cannot register resolved alias '" +
resolvedAlias + "' (original: '" + alias + "') for name '" + resolvedName +
"': It is already registered for name '" + registeredName + "'.");
}
this.aliasMap.put(resolvedAlias, resolvedName);
this.aliasMap.remove(alias);
}
else if (!registeredName.equals(resolvedName)) {
this.aliasMap.put(alias, resolvedName);
}
}
}
}
这个方法??
canonicalName方法
public String canonicalName(String name) {
String canonicalName = name;
// Handle aliasing.
String resolvedName = null;
do {
resolvedName = (String) this.aliasMap.get(canonicalName);
if (resolvedName != null) {
canonicalName = resolvedName;
}
}
while (resolvedName != null);
return canonicalName;
}
假设
aliasMap.put("a", "1");
aliasMap.put("1", "2");
aliasMap.put("2", "3");
调用canonicalName("a")的返回值为3