(spring-8th Back "IOC Foundation") beandefinition registration in IOC container

Source: Internet
Author: User

In spring, all beans are managed by beanfactory. The following is the Beanfactory class architecture:

We clearly see that defaultlistablebeanfactory inherited the fine tradition of beanfactory, and at the same time realized the Beandefinitionregistry this registrar, then undoubtedly, Beandefinition the registration task in the container, not his genus. In fact, Defaultlistablebeanfactory has a private Beandefinitonmap property, which is a hash map that is registered by putting the parsed Beandefiniton into the hash map. Here's how to register:

1 //---------------------------------------------------------------------2     //implementation of Beandefinitionregistry interface3     //---------------------------------------------------------------------4 5      Public voidregisterbeandefinition (String beanname, beandefinition beandefinition)6             throwsbeandefinitionstoreexception {7 8。。。。。。 +          A             This  . Beandefinitionmap.put (Beanname, beandefinition);  the              +  - resetbeandefinition (beanname); $         } $}

The 41st line takes beandefinition as value, and beanname as key, puts it into Beandefinitionmap, and completes the registration. So, where did this beandefinition come from?

Since my myeclipse environment uses the spring source rather than the jar package, so we do a little test, I configure a simple <bean>, and then do a bad simple spring container start and <bean> load, Then the program must go to 41 lines to perform the registration. I add a sentence on line 41st: Beandefinition=null, so the execution to the 41st line will be an error, the error message is as follows:

...   caused by:java.lang.NullPointerException at Java.util.concurrent.ConcurrentHashMap.put (concurrenthashmap.java:881) At   org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition ( defaultlistablebeanfactory.java:628) at Org.springframework.beans.factory.support.BeanDefinitionReaderUtils.registerBeanDefinition ( beandefinitionreaderutils.java:148)   at Org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.processBeanDefinition ( defaultbeandefinitiondocumentreader.java:263) at Org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement ( defaultbeandefinitiondocumentreader.java:153) at Org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions ( defaultbeandefinitiondocumentreader.java:132) at Org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions ( defaultbeandefinitiondocumentreader.java:93)  -At Org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions ( xmlbeandefinitionreader.java:493) -At Org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions ( xmlbeandefinitionreader.java:390) -... More

Looking backwards from line 26th is the process of loading and registering the entire beandefinition. The approximate process We at a glance:

1. Start loading and registering by Xmlbeandefinitionreader.

2. Start parsing by Defaultbeandefinitiondocumentreader.

3. Transition from Beandefinitionreaderutils.

4. Registered by Defaultlistablebeanfactory.

Let's take a concrete look at these four classes.

Interpreting the 1th step: Resource After the XML file is loaded, the first is parsed by Beandefinitionreader. Here are some of Beandefinitionreader's descendants and notes:

Interpretation of the 2nd step: Then the second step of the Defaultbeandefinitiondocumentreader should be the figure mentioned in the "Beandefinitiondocumentreader" of some descendants. It is responsible for the dialogue with Beandefinitionregistry's stakeholders. And this "Beandefinitionregistry" is defaultlistablebeanfactory.

The family map of Beandefinitiondocumentreader is also simple, and its family tree diagram illustrates:

Interpretation of the 3rd step: Transition class.

Interpreting the 4th step: The article has been analyzed at the outset. In fact, Defaultlistablebeanfactory can do a lot of things, in addition to registering beandefinition, it can be used as a separate beanfactory, but also can be inherited by the basic beanfactory subclasses, Open the API document you will see, Defaultlistablebeanfactory inherited and implemented a bunch of parents, father-in-the-father a lot of, the API described it as: "A full-fledged bean factory" (Plump bean Factory) , the following is its father and father-in-the-fathers:

Yes, in the spring kingdom, it is rich second generation.

(spring-8th Back "IOC Foundation") beandefinition registration in IOC container

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.