博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring源码分析(一)
阅读量:5933 次
发布时间:2019-06-19

本文共 7410 字,大约阅读时间需要 24 分钟。

一、首先分析AliasRegistry接口。

  1、Alias别名,Registry注册表,AliasRegistry别名注册表接口。

  2、共有四个方法,注册别名,判断是否别名,获取别名数组,移除别名。

  3、我自己试着写了一个这个接口的实现类:

package com.lzh.spring.test;import java.util.ArrayList;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Map.Entry;import org.springframework.core.AliasRegistry;/** * 别名注册表接口的实现 * @author LiaoZhengHan * @date 2018年8月29日 */public class MyAliasRegistry implements AliasRegistry{        private Map
aliasMap = new Hashtable<>(); @Override public void registerAlias(String name, String alias) { if (isAlias(alias)) { throw new IllegalStateException(); } aliasMap.put(alias, name); } @Override public void removeAlias(String alias) { aliasMap.remove(alias); } @Override public boolean isAlias(String name) { return aliasMap.containsKey(name); } @Override public String[] getAliases(String name) { List
aliases = new ArrayList<>(); Iterator
> it = aliasMap.entrySet().iterator(); Entry
entry = null; while (it.hasNext()) { entry = it.next(); if (entry.getValue().equals(name)) { aliases.add(entry.getKey()); } } String[] array = new String[aliases.size()]; return aliases.toArray(array); }}

 

/* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */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
aliasMap = new ConcurrentHashMap
(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 { if (!allowAliasOverriding()) { String registeredName = 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 + "'."); } } checkForAliasCircle(name, alias); this.aliasMap.put(alias, name); } } /** * Return whether alias overriding is allowed. * Default is {
@code true}. */ protected boolean allowAliasOverriding() { return true; } @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
result = new ArrayList
(); 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
result) { for (Map.Entry
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. *

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

aliasCopy = new HashMap
(this.aliasMap); for (String alias : aliasCopy.keySet()) { String registeredName = aliasCopy.get(alias); String resolvedAlias = valueResolver.resolveStringValue(alias); String resolvedName = valueResolver.resolveStringValue(registeredName); if (resolvedAlias.equals(resolvedName)) { this.aliasMap.remove(alias); } else if (!resolvedAlias.equals(alias)) { String existingName = 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 + "'."); } checkForAliasCircle(resolvedName, resolvedAlias); this.aliasMap.remove(alias); this.aliasMap.put(resolvedAlias, resolvedName); } else if (!registeredName.equals(resolvedName)) { this.aliasMap.put(alias, resolvedName); } } } } /** * 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; } /** * Check whether the given name points back to given alias as an alias * in the other direction, catching a circular reference upfront and * throwing a corresponding IllegalStateException. * @param name the candidate name * @param alias the candidate alias * @see #registerAlias */ protected void checkForAliasCircle(String name, String alias) { if (alias.equals(canonicalName(name))) { throw new IllegalStateException("Cannot register alias '" + alias + "' for name '" + name + "': Circular reference - '" + name + "' is a direct or indirect alias for '" + alias + "' already"); } }}

 

 

转载于:https://www.cnblogs.com/liaozhenghan/p/9552772.html

你可能感兴趣的文章
BZOJ 1193--马步距离
查看>>
Cookie、Session和自定义分页
查看>>
8086汇编语言 调用声卡播放wav文件(sound blaster)
查看>>
Java 面向对象
查看>>
java final static
查看>>
delphi公用函数
查看>>
java九阴真经
查看>>
数据结构和算法基础
查看>>
Spring事务管理2----编程式事务管理
查看>>
0417 jsBom操作+Dom再次整理
查看>>
面试中常见的 MySQL 考察难点和热点
查看>>
Oracle 导出空表的新方法(彻底解决)
查看>>
设置模式讲解-索引
查看>>
Cocos2d-x之Sequence动作序列执行
查看>>
第一期周二航拍视频分享 2017/07/10
查看>>
非聚集索引的注意事项
查看>>
springboot整合rabbirmq(3.7.9)中使用mandatory参数获取匹配失败的消息以及存入rabbitmq备份交换器中!...
查看>>
ES备份
查看>>
iOS项目日志1-联系人列表
查看>>
hdu 4194(模拟)
查看>>