(Spring-8th back to [IoC Basics]) BeanDefinition registration in the IoC container, beandefinition

Source: Internet
Author: User

(Spring-8th back to [IoC Basics]) BeanDefinition registration in the IoC container, beandefinition

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

We can clearly see that DefaultListableBeanFactory inherits the fine tradition of BeanFactory and implements the registration machine BeanDefinitionRegistry. Therefore, BeanDefinition is not a task of registering in the container. In fact, defalistlistablebeanfactory has a private BeanDefinitonMap attribute. This attribute is a hash Map. After the parsed BeanDefiniton is put into the hash Map, the registration is completed. The registration method is as follows:

 1 //--------------------------------------------------------------------- 2     // Implementation of BeanDefinitionRegistry interface 3     //--------------------------------------------------------------------- 4  5     public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) 6             throws BeanDefinitionStoreException { 7  8        。。。。。。40         41             this.beanDefinitionMap.put(beanName, beanDefinition);42             43 44             resetBeanDefinition(beanName);45         }46     }

In row 41st, beanDefinition is used as the value, and beanName is used as the key and put into beanDefinitionMap. The registration is completed. So where did the beanDefinition come from?

Because my myeclipse environment uses the spring source code, rather than the jar package, we conduct a small test. I configure a simple <bean>, then, when a simple spring container is started and <bean> loaded, the program will certainly go to 41 lines to execute registration. I add beanDefinition = null on line 1. In this case, an error will be reported when the code is executed in line 2. The error message is as follows:

。。。。。。17 Caused by: java.lang.NullPointerException18     at java.util.concurrent.ConcurrentHashMap.put(ConcurrentHashMap.java:881)19     at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:628)20     at org.springframework.beans.factory.support.BeanDefinitionReaderUtils.registerBeanDefinition(BeanDefinitionReaderUtils.java:148)21     at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.processBeanDefinition(DefaultBeanDefinitionDocumentReader.java:263)22     at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:153)23     at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:132)24     at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:93)25     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)26     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)27     ... 14 more

The entire beanDefinition loading and registration process is shown from the bottom of Line 1. The general process is clear at a glance:

1. XmlBeanDefinitionReader starts loading and registering.

2. parsing starts with DefaultBeanDefinitionDocumentReader.

3. Transition from BeanDefinitionReaderUtils.

4. registered by defalistlistablebeanfactory.

Let's analyze these four classes in detail.

Step 2: After a Resource is loaded with an XML file, it is first parsed by BeanDefinitionReader. Below are some child maps and comments of BeanDefinitionReader:

 

Interpretation Step 1: The DefaultBeanDefinitionDocumentReader mentioned in step 2 should be a descendant of "BeanDefinitionDocumentReader" mentioned in the figure. It is responsible for communicating with BeanDefinitionRegistry implementers. The "BeanDefinitionRegistry real-time" is DefaultListableBeanFactory.

BeanDefinitionDocumentReader's family chart is also very simple. Its Family Tree chart description:

Step 2: transition class.

Step 2: The article has been analyzed at the beginning. In fact, DefaultListableBeanFactory can do a lot of things. In addition to registering BeanDefinition, it can be used as an independent BeanFactory and inherited by the Basic BeanFactory subclass. You can see it when you open the API documentation, defaultListableBeanFactory inherits and implements a bunch of parent classes. It is described in the API as "a full-fledged bean factory" (The bean factory with full wings ), the following are their parents and parents:

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

 

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.