In this chapter we analyze how spring parses <bena></bean> generates Beandefinition and enters
//Org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader
1 Private voidparsedefaultelement (Element ele, beandefinitionparserdelegate delegate) {2 if(Delegate.nodenameequals (Ele, import_element)) {3 Importbeandefinitionresource (ele);//<import/>4 }5 Else if(Delegate.nodenameequals (Ele, alias_element)) {6 processaliasregistration (ele);//<alias/>7 }8 Else if(Delegate.nodenameequals (Ele, bean_element)) {9 processbeandefinition (Ele, delegate);//<bean/>Ten } One Else if(Delegate.nodenameequals (Ele, nested_beans_element)) { A //recurse - doregisterbeandefinitions (ele);//nested <beans/> - } the}
Line 9th, parse <bean/> node, enter Processbeandefinition method
//Org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader
1 /**2 * Process The given bean element, parsing the bean definition3 * and registering it with the registry.4 */5 protected voidprocessbeandefinition (Element ele, beandefinitionparserdelegate delegate) {6Beandefinitionholder Bdholder =delegate.parsebeandefinitionelement (ele);7 if(Bdholder! =NULL) {8Bdholder =delegate.decoratebeandefinitionifrequired (Ele, bdholder);9 Try {Ten //Register The final decorated instance. One beandefinitionreaderutils.registerbeandefinition (Bdholder, Getreadercontext (). GetRegistry ()); A } - Catch(Beandefinitionstoreexception ex) { -Getreadercontext (). Error ("Failed to register bean definition with Name '" + theBdholder.getbeanname () + "'", Ele, ex); - } - //Send Registration event. -Getreadercontext (). firecomponentregistered (Newbeancomponentdefinition (Bdholder)); + } -}
Line 6th, creating the Beandefinitionholder instance (this instance contains Beandefinition, Beanname, bean aliases)
Line 11th, register beandefinition, where Getreadercontext () Gets the Xmlreadercontext instance that was created in Xmlbeandefinitionreader through the Createreadercontext method. Getreadercontext (). GetRegistry () call is GetRegistry () of the Xmlbeandefinitionreader instance, Get is Beandefinitionregistry instance (Implementation class Defaultlistablebeanfactory)
//Org.springframework.beans.factory.xml.XmlBeanDefinitionReader
1 /**2 * Create the {@linkXmlreadercontext} to pass through to the document reader.3 */4 Publicxmlreadercontext Createreadercontext (Resource Resource) {5 return NewXmlreadercontext (Resource, This. Problemreporter, This. EventListener,6 This. Sourceextractor, This, Getnamespacehandlerresolver ());7}
Enter the Registerbeandefinition method
org.springframework.beans.factory.support.BeanDefinitionReaderUtils
1 /**2 * Register The given bean definition with the given Bean factory.3 * @paramDefinitionholder The bean definition including name and aliases4 * @paramregistry The Bean Factory to register with5 * @throwsbeandefinitionstoreexception If registration failed6 */7 Public Static voidRegisterbeandefinition (8 beandefinitionholder Definitionholder, beandefinitionregistry Registry)9 throwsbeandefinitionstoreexception {Ten One //Register Bean definition under primary name. AString Beanname =definitionholder.getbeanname (); - registry.registerbeandefinition (Beanname, Definitionholder.getbeandefinition ()); - the //Register aliases for beans name, if any. -string[] Aliases =definitionholder.getaliases (); - if(Aliases! =NULL) { - for(String aliase:aliases) { + Registry.registeralias (Beanname, aliase); - } + } A}
Line 12th, get Beanname
Line 13th, call Beandefinitionregistry (Implementation class is Defaultlistablebeanfactory) Registerbeandefinition method Register Beandefinition, enter method
//org.springframework.beans.factory.support.DefaultListableBeanFactory
1 //---------------------------------------------------------------------2 //implementation of Beandefinitionregistry interface3 //---------------------------------------------------------------------4 5 @Override6 Public voidregisterbeandefinition (String beanname, beandefinition beandefinition)7 throwsbeandefinitionstoreexception {8 9Assert.hastext (Beanname, "Bean name must not be empty");TenAssert.notnull (beandefinition, "beandefinition must not is null"); One A if(beandefinitioninstanceofabstractbeandefinition) { - Try { - ((abstractbeandefinition) beandefinition). Validate (); the } - Catch(Beandefinitionvalidationexception ex) { - Throw Newbeandefinitionstoreexception (Beandefinition.getresourcedescription (), Beanname, -"Validation of bean definition failed", ex); + } - } + A synchronized( This. Beandefinitionmap) { atBeandefinition oldbeandefinition = This. Beandefinitionmap.get (beanname); - if(Oldbeandefinition! =NULL) { - if(! This. allowbeandefinitionoverriding) { - Throw Newbeandefinitionstoreexception (Beandefinition.getresourcedescription (), Beanname, -"Cannot register bean definition [" + beandefinition + "] for bean '" + beanname + -"': There is already [" + Oldbeandefinition + "] bound."); in } - Else if(Oldbeandefinition.getrole () <Beandefinition.getrole ()) { to //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 bean '" + beanname + the"With a framework-generated bean definition ': replacing [" + *Oldbeandefinition + "] with [" + Beandefinition + "]"); $ }Panax Notoginseng } - Else { the if( This. logger.isinfoenabled ()) { + This. Logger.info ("Overriding bean definition for bean" + beanname + A"': replacing [" + Oldbeandefinition + "] with [" + Beandefinition + "]"); the } + } - } $ Else { $ This. Beandefinitionnames.add (beanname); - This. Frozenbeandefinitionnames =NULL; - } the This. Beandefinitionmap.put (Beanname, beandefinition); - }Wuyi the resetbeandefinition (beanname); -}
Line 46th, add Beanname to Beandefinitionnames (list<string> type)
Line 49th, put Beanname, beandefinition according to Key-value key value pairs into Beandefinitionmap (map<string, beandefinition> type)
Through the above analysis, we can already get all the Beanname and beandefinition, then when to create an instance of the bean, the next chapter is introduced.
Spring Source Parsing (v) parsing <bean></bean>