6.1-AliasRegistry
AliasRegistry
//AliasRegistry package org.springframework.core; /**
* Common interface for managing aliases. Serves as super-interface for
* {@link org.springframework.beans.factory.support.BeanDefinitionRegistry}.
*
* @author Juergen Hoeller
* @since 2.5.2
*/ /** 注册,移除,判断, 获取
**/ public interface AliasRegistry { /**
* Given a name, register an alias for it.
* @param name the canonical name
* @param alias the alias to be registered
* @throws IllegalStateException if the alias is already in use
* and may not be overridden
*/
//
void registerAlias(String name, String alias); /**
* Remove the specified alias from this registry.
* @param alias the alias to remove
* @throws IllegalStateException if no such alias was found
*/
void removeAlias(String alias); /**
* Determine whether this given name is defines as an alias
* (as opposed to the name of an actually registered component).
* @param name the name to check
* @return whether the given name is an alias
*/
boolean isAlias(String name); /**
* Return the aliases for the given name, if defined.
* @param name the name to check for aliases
* @return the aliases, or an empty array if none
*/
String[] getAliases(String name); }
SimpleAliasRegistry
//SimpleAliasRegistry package org.springframework.core; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.util.StringValueResolver; /**
* Simple implementation of the {@link AliasRegistry} interface.
* Serves as base class for
* {@link org.springframework.beans.factory.support.BeanDefinitionRegistry}
* implementations.
*
* @author Juergen Hoeller
* @since 2.5.2
*/ /**
实现了父接口中方法,并且增加了 **/
public class SimpleAliasRegistry implements AliasRegistry { //缓存别名和类名的地方
/** Map from alias to canonical name */
private final Map<String, String> aliasMap = new ConcurrentHashMap<String, String>(16); //注册的过程 检测-----》类名和别名是不是空的----->判断是否相等,,相等就移除--->否则使用别名获取实际名,存在---->直接结束
@Override
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 {
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 register alias '" + alias + "' for name '" +
name + "': It is already registered for name '" + registeredName + "'.");
}
}
checkForAliasCircle(name, alias);
this.aliasMap.put(alias, name);
}
} /**
* Return whether alias overriding is allowed.
* Default is {@code true}.
*/
protected boolean allowAliasOverriding() {
return true;
} /**
* Determine whether the given name has the given alias registered.
* @param name the name to check
* @param alias the alias to look for
* @since 4.2.1
*/
public boolean hasAlias(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();
return (registeredAlias.equals(alias) || hasAlias(registeredAlias, alias));
}
}
return false;
} @Override
public void removeAlias(String alias) {
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<String> result = new ArrayList<String>();
synchronized (this.aliasMap) {
retrieveAliases(name, result);
}
return StringUtils.toStringArray(result);
} /**
* Transitively retrieve all aliases for the given name.
* @param name the target name to find aliases for
* @param result the resulting aliases list
*/
private void retrieveAliases(String name, List<String> result) {
for (Map.Entry<String, String> entry : this.aliasMap.entrySet()) {
String registeredName = entry.getValue();
if (registeredName.equals(name)) {
String alias = entry.getKey();
result.add(alias);
retrieveAliases(alias, result);
}
}
} /**
* Resolve all alias target names and aliases registered in this
* factory, applying the given StringValueResolver to them.
* <p>The value resolver may for example resolve placeholders
* in target bean names and even in alias names.
* @param valueResolver the StringValueResolver to apply
*/
//批量检验别名
public void resolveAliases(StringValueResolver valueResolver) {
Assert.notNull(valueResolver, "StringValueResolver must not be null");
synchronized (this.aliasMap) {
Map<String, String> aliasCopy = new HashMap<String, String>(this.aliasMap);
for (String alias : aliasCopy.keySet()) {
String registeredName = aliasCopy.get(alias);
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);
break;
}
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);
}
}
}
} /**
* Check whether the given name points back to the given alias as an alias
* in the other direction already, catching a circular reference upfront
* and throwing a corresponding IllegalStateException.
* @param name the candidate name
* @param alias the candidate alias
* @see #registerAlias
* @see #hasAlias
*/
//检查别名是否存在
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");
}
} /**
* Determine the raw name, resolving aliases to canonical names.
* @param name the user-specified name
* @return the transformed 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;
} }
实现类:
registerAlias(String name, String alias) -----注册别名的方法,其实这个注册完的别名是放在了一个Map中 注册的过程:这个图是我从别的地方看到的,没画,其实可以看代码,很明了
方法:public String[] getAliases(String name) 这个方法是获取别名的,其中使用了递归调用,使用了同步锁,
@Override
public String[] getAliases(String name) {
List<String> result = new ArrayList<String>();
synchronized (this.aliasMap) {
retrieveAliases(name, result);
}
return StringUtils.toStringArray(result);
}
递归部分:
private void retrieveAliases(String name, List<String> result) {
for (Map.Entry<String, String> entry : this.aliasMap.entrySet()) {
String registeredName = entry.getValue();
if (registeredName.equals(name)) {
String alias = entry.getKey();
result.add(alias);
retrieveAliases(alias, result);
}
}
}
6.1-AliasRegistry的更多相关文章
- Spring之28:AliasRegistry&SimpleAliasRegistry
AliasRegistry接口定义了alias的基本操作. package org.springframework.core; public interface AliasRegistry { //对 ...
- 死磕Spring源码之AliasRegistry
死磕Spring源码之AliasRegistry 父子关系 graph TD; AliasRegistry-->BeanDefinitionRegistry; 代码实现 作为bean定义的最顶层 ...
- AliasRegistry接口
Spring - 4.2.3 // 将一个name注册为一个别名aliasvoid registerAlias(String name, String alias);// 移除一个别名aliasvoi ...
- BeanDefinitionRegistry extends AliasRegistry
// 用该Registry注册一个新定义的bean,但是新的bean必须支持父的定义和子的定义void registerBeanDefinition(String beanName, BeanDefi ...
- SimpleAliasRegistry implements AliasRegistry
Spring - 4.2.3 // name,alias存储容器 ConcurrentHashMap <alias,name>private final Map<String, St ...
- Spring源码分析——BeanFactory体系之抽象类、类分析(一)
上一篇介绍了BeanFactory体系的所有接口——Spring源码分析——BeanFactory体系之接口详细分析,本篇就接着介绍BeanFactory体系的抽象类和接口. 一.BeanFactor ...
- Spring源码分析——BeanFactory体系之接口详细分析
Spring的BeanFactory的继承体系堪称经典.这是众所周知的!作为Java程序员,不能错过! 前面的博文分析了Spring的Resource资源类Resouce.今天开始分析Spring的I ...
- 深入剖析 Spring 框架的 BeanFactory
说到Spring框架,人们往往大谈特谈一些似乎高逼格的东西,比如依赖注入,控制反转,面向切面等等.但是却忘记了最基本的一点,Spring的本质是一个bean工厂(beanFactory)或者说bean ...
- org.springframework.beans包
beans包中最核心的两个类:DefaultListableBeanFactory&XmlBeanDefinitionReader DefaultListableBeanFactory Xml ...
- Spring源码之SimpleAliasRegistry解读(一)
Spring源码之SimpleAliasRegistry解读(一) 阅读spring源码中org.springframework.core.SimpleAliasRegistry类时发现该类主要是使用 ...
随机推荐
- 码云的GIT操作
git操作 git initgit add .git commit -m ""git remote add origin https://git.coding.net/jessei ...
- 编译spark源码及塔建源码阅读环境
编译spark源码及塔建源码阅读环境 (一),编译spark源码 1,更换maven的下载镜像: <mirrors> <!-- 阿里云仓库 --> <mirror> ...
- Visual Prolog 的 Web 专家系统 (10)
GENI的核心 -- 推理机(4)求证过程分析 1.GENI知识库结构 专家系统推理机的设计执行,与其知识库结构紧密相关. GENI知识库结构是一棵逻辑推理树. 根节点是animal,即求证的目标. ...
- nginx的buffered to a temporary警告
nginx日志报a client request body is buffered to a temporary file 这个意思是客户全请求的文件超过了nginx的缓存区大小,nginx将内容写入 ...
- 2、cas4.0 单点登录 之 cas-client
cas4.0 单点登录 之 cas-client cas4.0 单点登录 之 https证书已经做好了证书的准备工作.如今结合cas-server来配置单点登录: 一.安装cas服务端(cas-ser ...
- select、poll、epoll之间的区别总结[整理](转)
select,poll,epoll都是IO多路复用的机制.I/O多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作.但select ...
- html头部转码
学习html是件比较容易的事情,但单单学html语言肯定是不够用的,所以大多数人并没有拿html作为学习核心,而是将html作为javascript.动态语言或者css学习的必经之路.于是很多人并不关 ...
- Ctrl/Alt 快捷键
Ctrl+S 保存 Ctrl+W 关闭程序 Ctrl+N 新建文档 Ctrl+O 打开(选择打开其他文档) Ctrl+Z 撤销 Ctrl+F 查找 Ctrl+X 剪切 Ct ...
- C#常见的概念阐述
在上篇文章中,你跟着我写了一个HelloWorld,本篇中,我们来谈谈一些C#程序中的小概念 1.C# 程序结构 一个 C# 程序主要包括以下部分: 命名空间声明(Namespace declarat ...
- C# readonly和const的不同以及它的具体用法
在C#中,我们用const来定义常量.常量就是我定义一个变量,这个变量的值在整个软件的生命周期都不变.比如我想求一个圆形的面积,我就可以把π定义成一个常量,因为我事先知道圆周率是就是3.1415926 ...