Spring Source Code Analysis-xmlbeanfactory Introduction

Source: Internet
Author: User
Source code analysis is a painful and happy thing. Reading the code written by others is successful, but when you can understand it, I believe that happiness will also come along, in order to reduce the pain and bring happiness more quickly, I hope this article will help my friends who find it difficult.

This article analyzes the xmlbeanfactory in the Spring framework and hopes to provide some help to friends who need it in a concise and clear way.

First, open the code for this class. We will see the following code:
The following is the program code:

Public class xmlbeanfactory extends defaultlistablebeanfactory {

Private Final xmlbeandefinitionreader reader = new xmlbeandefinitionreader (this );

Public xmlbeanfactory (resource Resource) throws beansexception {
This (resource, null );
}

Public xmlbeanfactory (resource, beanfactory parentbeanfactory) throws beansexception {
Super (parentbeanfactory );
This. Reader. loadbeandefinitions (Resource );
}

}

The code of this class is very simple. A member object is added with two constructor functions. From this we can see that the most important thing is the last constructor:

The following is the program code:

Super (parentbeanfactory );
This. Reader. loadbeandefinitions (Resource );


The first sentence is to hand over the parent factory to the constructor of the parent class. In fact, the parent factory is saved to the parentbeanfactory member object of the class.
Abstractbeanfactory defined in the abstract class, and this parent factory will always be passed to this abstract class for saving. The second sentence is the most important part of the entire class. As the name suggests, it
The purpose is to read the bean definition from the resource Resource (that is, your configuration file) through xmlbeandefinitionreader.
. Next we open the loadbeandefinitionreader loadbeandefinitions method, and we can see that there is a line of code in this method,
A Method with different parameters of the same name is called, and the parameter is an instance of encodedresource. This class is actually a packaging class of resource, used to save the resource
Encode, then let's look at the called loadbeandefinitions method. The main part of this method is:

The following is the program code:

Inputsource = new inputsource (inputstream );
If (encodedresource. getencoding ()! = NULL ){
Inputsource. setencoding (encodedresource. getencoding ());
}
Return doloadbeandefinitions (inputsource, encodedresource. getresource ());

The purpose is to package the resource into an inputsource, and pass the resource as a parameter to the doloadbeandefinitions method.

Program code:
Document. uilderfactory factory = createdocument. uilderfactory ();
If (logger. isdebugenabled ()){
Logger. debug ("using JAXP implementation [" + factory + "]");
}
Document. uilder builder = createdocument. uilder (factory );
Document. nbsp; Doc = builder. parse (inputsource );
Return registerbeandefinitions (Doc, resource );


The purpose of this method is to interpret the resource as a document object and then call the registerbeandefinitions method.
For more information, see Introduction to JAXP. Next, open the registerbeandefinitions method:
The following is the program code:

Public int registerbeandefinitions (document DOC, resource Resource) throws beansexception {
Xmlbeandefinitionparser parser =
(Xmlbeandefinitionparser) beanutils. instantiateclass (this. parserclass );
Return parser. registerbeandefinitions (this, Doc, resource );
}


An xmlbeandefinitionparser interface is created here. The specific class of this interface is
Defaultxmlbeandefinitionparser. This interface is very simple. Only registerbeandefinitions has one method.
The role of the method is also very clear, it is used to register the bean definition, so the class and method names must be meaningful, so that people can understand its role at a glance, reduces the pain points of reading code.
Bitter. Let's just talk about it. Let's open the registerbeandefinitions method of defaxmlxmlbeandefinitionparser. This class
It is the core class of the XML configuration file. After opening the registerbeandefinitions method, we can see the following code:
The following is the program code:

Public int registerbeandefinitions (beandefinitionreader, document DOC, resource Resource)
Throws beandefinitionstoreexception {

This. beandefinitionreader = reader;
This. Resource = resource;

Logger. debug ("loading bean definitions ");
Element root = Doc. getdocumentelement ();
// Initialize the root element
Initdefaults (Root );
If (logger. isdebugenabled ()){
Logger. debug ("Default lazy init '" + getdefalazyinit () + "'");
Logger. debug ("Default autowire" + getdefaautoautowire () + "'");
Logger. debug ("Default dependency check'" + getdefadependencycheck () + "'");
}

Preprocessxml (Root); // an empty METHOD FOR EXTENSION
Int beandefinitioncount = parsebeandefinitions (Root); // describes the configuration methods.
If (logger. isdebugenabled ()){
Logger. debug ("found" + beandefinitioncount + "<bean> elements in" + Resource );
}
Postprocessxml (Root); // an empty METHOD FOR EXTENSION

Return beandefinitioncount;
}


In this method, two methods are mainly used to explain the definition. One is initdefaults, the other is parsebeandefinitions, and the first method is used
Attribute of the root element, such as lazy-init,
While parsebeandefinitions is used to explain the specific bean definition. The method code is as follows:
The following is the program code:

Protected int parsebeandefinitions (element root) throws beandefinitionstoreexception {
Nodelist NL = root. getchildnodes ();
Int beandefinitioncount = 0;
For (INT I = 0; I <NL. getlength (); I ++ ){
Node node = NL. item (I );
If (node instanceof element ){
Element ele = (element) node;
If (import_element.equals (node. getnodename ())){
Importbeandefinitionresource (Ele );
}
Else if (alias_element.equals (node. getnodename ())){
String name = ELE. getattribute (name_attribute );
String alias = ELE. getattribute (alias_attribute );
This. beandefinitionreader. getbeanfactory (). registeralias (name, alias );
}
Else if (bean_element.equals (node. getnodename ())){
Beandefinitioncount ++;
Beandefinitionholder bdholder = parsebeandefinitionelement (Ele, false );

Beandefinitionreaderutils. registerbeandefinition (bdholder,
This. beandefinitionreader. getbeanfactory ());
}
}
}
Return beandefinitioncount;
}

Here we will not talk much about how other labels are explained. I believe you can understand them as well. Here we will mainly talk about bean processing. We should pay attention to the following code:
The following is the program code:

Else if (bean_element.equals (node. getnodename ())){
Beandefinitioncount ++;
Beandefinitionholder bdholder = parsebeandefinitionelement (Ele, false );

Beandefinitionreaderutils. registerbeandefinition (bdholder,
This. beandefinitionreader. getbeanfactory ());
}


Here is the processing performed when a bean label is encountered. It is both an explanation of bean definition. We can see that parsebeandefinitionelement
The first parameter of the method is the bean element, and the second parameter indicates whether the bean is a built-in bean. The bean interpreted here cannot be built-in.
False is a parameter. Open the parsebeandefinitionelement method and you can see that this method is an internal explanation of the bean, which is also very simple and not much
I 've talked about it, huh, huh (it's time to get off work, so I 've written so much about it, and the basic process is like this. There's nothing really hard to do .), By the way, the bean definition will be
Save it to beanfactory. The implementation of this beanfactory is xmlbeanfactory, which is passed at New
This is the following line of code in the class:
The following is the program code:

Private Final xmlbeandefinitionreader reader = new xmlbeandefinitionreader (this );

Okay, that's all. This article serves only as a reference and only explains how to load bean definitions. It serves only as a reference. I hope it will be helpful to other friends because of the rush of time, correct the error.

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.