---restore content starts---
Objective
One of the codes in the previous article was the entrance to Beandefiniton registration, and we looked back.
1.BeanDefiniton Registration in IOC container
First we review two points,1. Place of registration; 2. Registered Implementation Class
1. Where registration is initiated
Let's look at the 1th, in the Last post we talked about Bean parsing, in the Processbeandefiniton () method of the Defaultbeandefinitondocumentreader class, One of them is to complete the Beandefiniton loading, and the other is to complete the registration. Let's look at the code:
The Processbeandefinition method of the Defaultbeandefinitiondocumentreader class
protected voidprocessbeandefinition (Element ele, beandefinitionparserdelegate delegate) { //parsing method
Beandefinitionholder Bdholder=delegate.parsebeandefinitionelement (ele); if(Bdholder! =NULL) {Bdholder=delegate.decoratebeandefinitionifrequired (Ele, Bdholder); Try { //Registration method
beandefinitionreaderutils.registerbeandefinition (Bdholder, Getreadercontext (). GetRegistry ()); } Catch(Beandefinitionstoreexception ex) {Getreadercontext (). Error ("Failed to register bean definition with name" +Bdholder.getbeanname ()+ "'", Ele, ex); } Getreadercontext (). firecomponentregistered (Newbeancomponentdefinition (Bdholder)); } }
Now that we have found Beandefiniton's registration portal, we know Beandefinitonholder=beandefiniton+beanname, and look at the Registerbeandefinition code:
the Registerbeandefinition method of Beandefinitionreaderutils class
Public Static voidregisterbeandefinition (Beandefinitionholder Definitionholder, beandefinitionregistry Registry) throwsbeandefinitionstoreexception {//Register Beandefinition according to BeannameString Beanname =Definitionholder.getbeanname (); Registry.registerbeandefinition (Beanname, Definitionholder.getbeandefinition ()); //If there is an alias, register the alias
string[] Aliases =definitionholder.getaliases (); if(Aliases! =NULL) { for(String alias:aliases) {Registry.registeralias (beanname, alias); } } }
2 Registered Implementation Class
We find the implementation of Registerbeandefinition class, we will find that there are three implementation classes, then specifically take which one? Remember what container we used when we first built the container? Let's review the code:
The Refreshbeanfactory method of Abstractrefreshableapplicationcontext class
protected Final voidRefreshbeanfactory ()throwsbeansexception {if(Hasbeanfactory ()) {Destroybeans (); Closebeanfactory (); } Try{defaultlistablebeanfactory beanfactory=createbeanfactory (); Beanfactory.setserializationid (GetId ()); Customizebeanfactory (beanfactory); Loadbeandefinitions (beanfactory); synchronized( This. Beanfactorymonitor) { This. Beanfactory =beanfactory; } } Catch(IOException ex) {Throw NewApplicationcontextexception ("I/O error parsing bean definition source for" +GetDisplayName (), ex); } }
Furthermore, we see that Defaultlistablebeanfactory implements the Beandefinitionregistry interface, which is the definition of registration . And there is such a hashmap in this class, where the Beandefiniton is stored.
//--------------------------------------------------------------------- //implementation of Beandefinitionregistry interface//---------------------------------------------------------------------@Override Public voidregisterbeandefinition (String beanname, beandefinition beandefinition)throwsbeandefinitionstoreexception {assert.hastext (beanname,"Bean name must not be empty"); Assert.notnull (Beandefinition,"Beandefinition must not being null"); if(beandefinitioninstanceofabstractbeandefinition) { Try{((abstractbeandefinition) beandefinition). Validate (); } Catch(Beandefinitionvalidationexception ex) {Throw Newbeandefinitionstoreexception (Beandefinition.getresourcedescription (), Beanname,"Validation of bean definition failed", ex); }} beandefinition oldbeandefinition; Oldbeandefinition= This. Beandefinitionmap.get (Beanname); if(Oldbeandefinition! =NULL) { if(!isallowbeandefinitionoverriding ()) { Throw Newbeandefinitionstoreexception (Beandefinition.getresourcedescription (), Beanname,"Cannot register bean definition [" + beandefinition + "] for beans '" + beanname + "': there is ALR Eady ["+ Oldbeandefinition +"] bound. "); } Else if(Oldbeandefinition.getrole () <Beandefinition.getrole ()) { //e.g. was role_application, now overriding with Role_support or role_infrastructure if( This. logger.iswarnenabled ()) { This. Logger.warn ("overriding user-defined bean definition for beans '" + beanname + "' with a Framew ork-generated Bean definition:replacing ["+oldbeandefinition+ "] with [" + Beandefinition + "]"); } } Else if(!beandefinition.equals (oldbeandefinition)) { if( This. logger.isinfoenabled ()) { This. Logger.info ("Overriding bean Definition for beans '" + beanname + "' with a different definitio n:replacing ["+ Oldbeandefinition +"] with ["+ Beandefinition +"] "); } } Else { if( This. logger.isdebugenabled ()) { This. Logger.debug ("Overriding bean Definition for beans '" + beanname + "' with an equivalent Defini tion:replacing ["+ Oldbeandefinition +"] with ["+ Beandefinition +"] "); } } This. Beandefinitionmap.put (Beanname, beandefinition); } Else { if(hasbeancreationstarted ()) {//cannot modify Startup-time collection elements anymore (for stable iteration) synchronized( This. Beandefinitionmap) { This. Beandefinitionmap.put (Beanname, beandefinition); List<String> updateddefinitions =NewArraylist<string> ( This. Beandefinitionnames.size () + 1); Updateddefinitions.addall ( This. Beandefinitionnames); Updateddefinitions.add (Beanname); This. Beandefinitionnames =updateddefinitions; if( This. Manualsingletonnames.contains (Beanname)) {Set<String> updatedsingletons =NewLinkedhashset<string> ( This. Manualsingletonnames); Updatedsingletons.remove (Beanname); This. Manualsingletonnames =updatedsingletons; } } } Else { //still in startup registration phase This. Beandefinitionmap.put (Beanname, beandefinition); This. Beandefinitionnames.add (Beanname); This. Manualsingletonnames.remove (Beanname); } This. Frozenbeandefinitionnames =NULL; } if(Oldbeandefinition! =NULL||Containssingleton (Beanname)) {resetbeandefinition (beanname); } }
Here, the code completes the process of initializing the IOC container. At this point, the configuration information for the entire bean has been established in the IOC container defaultlistablebeanfactory used, and these beandefinition can already be used by the container. They are all retrieved and used in the Beandefinitionmap.
---restore content ends---
Initialization of the Spring IOC container-(iii) registration of Beandefinition