(spring-6th) beandefinition--The first big weapon to instantiate a bean.

Source: Internet
Author: User

The last section tells the internal mechanism of the bean instantiation, which is repeated here again:

  1. Resourceloader loads XML configuration information from the system and is represented by resource.
  2. Beandefinitionreader reads the configuration information from the resource, parses the <bean> in the configuration file into a Beandefinition object , Then put the Beandefinition object in the Beandefinitionregistry registry.
  3. The container scans the Bean Factory post processor's bean from the Beandefinitionregistry registry (which implements the Beanfactorypostprocessor) and uses the factory post-processor to Processes all Beandefinition objects in the Beandefinitionregistry registry . Specifically, two things were done:
    1. The bean that uses the placeholder of the <bean> element is parsed, the placeholder is converted into a concrete value, and the Beandefinition object of the semi-finished product is transformed into the finished object.
    2. Scans all Beandefinition objects in the Beandefinitionregistry registry, and finds all the property editor's beans through the Java reflection mechanism (implements the PropertyEditor bean). Then put it in the Property Editor registry (Propertyeditorregistry).
  4. The container takes the processed beandefinitionout of the beandefinitionregistry and invokes Instantiationstrategy to start the bean instantiation work.
  5. When you instantiate a bean, the spring container uses Beanwrapper to encapsulate the bean, Beanwrapper with the Beandefinition and the property editor to do the Bean property setup work.
  6. Subsequent processing of the bean generated by the fifth step is made using the Bean post processor registered in the container (which implements Beanpostprocessor).

From the process of instantiation, it can be seen that beandefinition plays a mainstay role. Because Beandefinition is the internal representation of the configuration file <bean> element label in the container. For example,,<bean> tags have class,scope,lazy-init attributes in XML, then there are corresponding Beanclass,scope,lazyinit attributes in Beandefinition.

Inheritance structure of the Beandefinition interface

The top beandefinition is actually an interface, the following abstractbeandefinition implements this interface, And the bottom of the childbeandefinition and rootbeandefinition respectively inherit the abstractbeandefinition. Look at an XML configuration:

1  <!--Parent <bean> -2     <BeanID= "Car0"class= "Com.baobaotao.tagdepend.Car" 3 P:brand= "Red Flag CA72"P:price= "2000.00"P:color= "BLACK"4 Abstract= "true"/>5           6     <BeanID= "Car3"Parent= "Abstractcar">7         < Propertyname= "Color"value= "Red"/>8     </Bean>        

Car3 inherits the CAR0, corresponding to the inheritance structure diagram, CAR3 uses rootbeandefinition, that is, CAR3 has more properties than car0, whether car3 or CAR0, They all have commonalities (even if they are not written in the configuration file, they are invisible, such as the scope,lazyinit mentioned above), and these commonalities will be extracted in abstractbeandefinition. If the <bean> tag does not have an inheritance relationship, it will use the default rootbeandefinition, which adds a genericbeandefinition after version 2.5, because of its own advantage and becomes the default usage class.

Here is the API for Beandefinition: (online documentation: http://tool.oschina.net/apidocs/apidoc?api=Spring-3.1.1)

In the method overview, we can see that this interface defines all the <bean> properties of the method interface, such as Singleton, Prototype,lazyinit, and so on.


The following is the structure diagram and API for Abstractbeandefinition:

As there are many methods in the API, I take a few examples:

The Get method is to get the property values of the common class, in fact, the default value of the common attribute is given by default when defining the member variable, see the code:

1 Private volatileObject Beanclass;2 3     PrivateString scope =Scope_default;4 5     Private BooleanSingleton =true;6 7     Private BooleanPrototype =false;8 9     Private BooleanAbstractflag =false;Ten  One     Private BooleanLazyinit =false; A  -     Private intAutowiremode =Autowire_no; -  the     Private intDependencycheck =Dependency_check_none; -  -     Privatestring[] dependsOn; -  +     Private BooleanAutowirecandidate =true; -  +     Private BooleanPrimary =false;

The above code intercepts the source of the abstractbeandefinition, you can see many <bean> tag properties default values.

Beandefinition created at the beginning because the placeholder is a semi-finished product, the bean needs to be processed by the Bean Factory post processor, and the beandefinition is instantiated by instantiationstrategy after processing. We will introduce instantiationstrategy in detail later.

How is beandefinition created, and how is XML parsed? These questions need to go deep into the source to find answers, after the IOC part, I will analyze the source code, in-depth exploration of its mystery, I hope you continue to support and follow my blog.

(spring-6th) beandefinition--The first big weapon to instantiate a bean.

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.