Spring Source Analysis (i)

Source: Internet
Author: User

First, the analysis of the Aliasregistry interface.

1, alias aliases, Registry registry, Aliasregistry alias registry interface.

2, a total of four methods, register aliases, the name of the alias, get an array of aliases, remove aliases.

3, I myself tried to write an implementation class for this interface:

 Packagecom.lzh.spring.test;Importjava.util.ArrayList;Importjava.util.Hashtable;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Map.Entry;ImportOrg.springframework.core.AliasRegistry;/*** Implementation of the Alias registry interface *@authorLiaozhenghan * @date August 29, 2018*/ Public classMyaliasregistryImplementsaliasregistry{Privatemap<string, string> aliasmap =NewHashtable<>(); @Override Public voidRegisteralias (string name, string alias) {if(Isalias (alias)) {Throw Newillegalstateexception ();    } aliasmap.put (alias, name); } @Override Public voidRemovealias (String alias) {aliasmap.remove (alias); } @Override Public BooleanIsalias (String name) {returnAliasmap.containskey (name); } @Override Publicstring[] getaliases (String name) {List<String> aliases =NewArraylist<>(); Iterator<entry<string, string>> it =Aliasmap.entryset (). iterator (); Entry<string, string> entry =NULL;  while(It.hasnext ()) {entry=It.next (); if(Entry.getvalue (). Equals (name)) {Aliases.add (Entry.getkey ()); }} string[] array=Newstring[aliases.size ()]; returnAliases.toarray (array); }}

/** Copyright 2002-2012 the original author or authors. * Licensed under the Apache License, Version 2.0 (the "Licens E "); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable or agreed to writing, software * Distributed under the License is distribute D on ' 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. */ PackageOrg.springframework.core;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportJava.util.concurrent.ConcurrentHashMap;ImportOrg.springframework.util.Assert;Importorg.springframework.util.StringUtils;ImportOrg.springframework.util.StringValueResolver;/*** Simple Implementation of the {@linkAliasregistry} interface. * Serves as base class for * {@linkOrg.springframework.beans.factory.support.BeanDefinitionRegistry} * implementations. * * @authorJuergen Hoeller *@since2.5.2*/ Public classSimplealiasregistryImplementsAliasregistry {/**Map from alias to canonical name*/    Private Finalmap<string, string> aliasmap =NewConcurrenthashmap<string, String> (16); @Override Public voidRegisteralias (string name, string alias) {Assert.hastext (name,"' name ' must not be empty"); Assert.hastext (alias,"' Alias ' must not being 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 NewIllegalStateException ("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 {@codetrue}. */    protected Booleanallowaliasoverriding () {return true; } @Override Public voidRemovealias (String alias) {string name= This. Aliasmap.remove (alias); if(Name = =NULL) {            Throw NewIllegalStateException ("No alias" + alias + "' Registered"); }} @Override Public BooleanIsalias (String name) {return  This. Aliasmap.containskey (name); } @Override Publicstring[] getaliases (String name) {List<String> result =NewArraylist<string>(); synchronized( This. Aliasmap)        {retrievealiases (name, result); }        returnStringutils.tostringarray (Result); }    /*** transitively Retrieve all aliases for the given name. * @paramName the target name to find aliases for *@paramresult the resulting aliases list*/    Private voidRetrievealiases (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 Stringvaluereso     Lver to them.     * <p>the Value Resolver is example resolve placeholders * in target beans names and even in alias names. * @paramValueresolver the stringvalueresolver to apply*/     Public voidresolvealiases (Stringvalueresolver valueresolver) {assert.notnull (Valueresolver,"Stringvalueresolver must not being null"); synchronized( This. Aliasmap) {Map<string, string> aliascopy =NewHashmap<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.equals (resolvedname)) { This. Aliasmap.remove (alias); }                Else if(!Resolvedalias.equals (alias)) {String ExistingName= This. Aliasmap.get (Resolvedalias); if(ExistingName! =NULL&&!existingname.equals (Resolvedname)) {                        Throw NewIllegalStateException ("Cannot register resolved alias '" + Resolvedalias + "' (Original: ' + alias + ') for Nam E ' "+ 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. * @paramName the user-specified name *@returnThe transformed name*/     Publicstring CanonicalName (string name) {string CanonicalName=name; //Handle aliasing ...String Resolvedname;  Do{resolvedname= This. Aliasmap.get (CanonicalName); if(Resolvedname! =NULL) {canonicalname=Resolvedname; }        }         while(Resolvedname! =NULL); returncanonicalname; }    /*** Check Whether the given name points back to given alias as an alias * in the other direction, catching a CI     Rcular reference upfront and * throwing a corresponding illegalstateexception. * @paramName the candidate name *@paramAlias the candidate alias *@see#registerAlias*/    protected voidcheckforaliascircle (string name, string alias) {if(Alias.equals (canonicalname (name))) {Throw NewIllegalStateException ("Cannot register alias" + alias + "' for Name '" + Name + "': Circular referenc E-' "+name+ "is a direct or indirect alias for '" + Alias + "' Already"); }    }}


Spring Source Analysis (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.