Spring source parsing--starting from Xmlbeanfactory's constructor function to see Loadbeandefinitions

Source: Internet
Author: User
Tags assert


the previous article talked about the Classpathresource class, and through this class we loaded our spring configuration file from Classpath, and then we started to execute the xmlbeanfactory construction process:

Public Xmlbeanfactory (Resource Resource, Beanfactory parentbeanfactory) throws beansexception {super ( Parentbeanfactory); this.reader.loadBeanDefinitions (Resource);}


Let's look at our super method first:


/** * Create a new abstractautowirecapablebeanfactory. */public Abstractautowirecapablebeanfactory () {super (); Ignoredependencyinterface (Beannameaware.class); Ignoredependencyinterface (Beanfactoryaware.class); Ignoredependencyinterface (Beanclassloaderaware.class);}

the main function of Ignoredependencyinterface is to ignore the automatic assembly function of a given interface.


Method Prototypes:


public void Ignoredependencyinterface (class<?> ifc) {this.ignoredDependencyInterfaces.add (IFC);}

The item is actually a set:


/** * Dependency interfaces to ignore on Dependency check and Autowire, as Set of * Class objects. By default, only the Beanfactory interface is ignored. */private final set<class<?>> ignoreddependencyinterfaces = new hashset<class<?>> ();


For example, I've joined Beannameaware.class,beanfactoryaware.class,beanclassloaderaware.class here, and if there's a case for attribute B in a, when we get a, If B is not initialized, B is initialized by default, but, for example, B implements any of the above three interfaces, it does not automatically inject B, and instead injects B in other ways.


trace the Super method back to the Xmlbeanfactory constructor and execute this.reader.loadBeanDefinitions (Resource), which is the entry point for the entire resource load.


/** * Load Bean definitions from the specified XML file. * @param resource The Resource descriptor for the XML file * @return The number of beans definitions found * @throws Beande Finitionstoreexception in case of loading or parsing errors */public int loadbeandefinitions (Resource Resource) throws Bea ndefinitionstoreexception {return loadbeandefinitions (new Encodedresource (Resource));}

In the Loadbeandefinitions method, it calls its own constructor to encapsulate the incoming resource:


public class Encodedresource {private final Resource resource;private String encoding;private Charset charset;/** * Create A new encodedresource for the given Resource, * not specifying a specific encoding. * @param resource The resource to hold */public Encodedresource (resource Resource) {Assert.notnull (Resource, "resource Mus T is null "); this.resource = resource;} /** * Create A new encodedresource for the given Resource, * using the specified encoding. * @param resource The resource-to-hold * @param encoding the encoding-to-use-reading from the resource */public Encode Dresource (Resource Resource, String encoding) {Assert.notnull (Resource, "Resource must not is null"); This.resource = Reso urce;this.encoding = encoding;} /** * Create A new encodedresource for the given Resource, * using the specified encoding. * @param resource The resource-to-hold * @param charset the CharSet-to-use-reading from the resource */public Encodedr Esource (Resource Resource, Charset Charset) {Assert. Notnull (Resource, "resource must not is null"); This.resource = Resource;this.charset = CharSet;} 

With the two member variables encoding and charset, we found that we just wrapped up the coded stuff for our resource class.


Then formally enter the Loadbeandefinitions method:


/*1, first to the incoming resource do encapsulation, programming encodedresource, this object may be encoded. 2, prepare the InputSource object 3 by Sax reading the XML file, and finally pass the prepared data into the real core processing part doloadbeandefinitions (InputSource, Encodedresource.getresource ()); *//** * Load Bean definitions from the specified XML file. * @param encodedresource The resource descriptor for the XML file, * allowing to specify a encoding to use for parsing th  E file * @return The number of beans definitions found * @throws beandefinitionstoreexception in case of loading or parsing Errors */public int loadbeandefinitions (Encodedresource encodedresource) throws Beandefinitionstoreexception { Assert.notnull (Encodedresource, "Encodedresource must not is null"), if (logger.isinfoenabled ()) {Logger.info ("Loading XML Bean Definitions from "+ encodedresource.getresource ());} /* The goods are a ThreadLocal:resourcesCurrentlyBeingLoaded:private final threadlocal<set<encodedresource>> resourcescurrentlybeingloaded =new namedthreadlocal<set<encodedresource>> ("XML bean definition Resources currently Being loaded "); Use this property to record the resources that have been loaded, and if duplicates are found during loading, report an exception */set<encodedresource> currentresources = This.resourcesCurrentlyBeingLoaded.get (); if (currentresources = = null) {currentresources = new hashset< Encodedresource> (4); This.resourcesCurrentlyBeingLoaded.set (currentresources);} if (!currentresources.add (Encodedresource)) {throw new Beandefinitionstoreexception ("detected cyclic loading of" + Encodedresource + "-Check your import definitions!");} try {/* InputStream input flow to InputSource after entering Doloadbeandefinitions method */inputstream InputStream = Encodedresource.getresource (). getInputStream (); try {inputsource inputsource = new InputSource (InputStream); if ( Encodedresource.getencoding ()! = null) {inputsource.setencoding (encodedresource.getencoding ());} Return Doloadbeandefinitions (InputSource, Encodedresource.getresource ());} finally {inputstream.close ();}} catch (IOException ex) {throw new Beandefinitionstoreexception ("IOException parsing XML document from" + Encodedresource. GetResource (), ex);}finally {currentresources.remove (Encodedresource); if (Currentresources.isempty ()) { This.resourcesCurrentlyBeingLoaded.remove ();}}}

After entering into the Doloadbeandefinitions method:


/** * Actually load bean definitions from the specified XML file.  * @param inputsource the SAX inputsource to read from * @param resource the Resource descriptor for the XML file * @return The number of the bean definitions found * @throws beandefinitionstoreexception in case of loading or parsing errors */protec Ted Int Doloadbeandefinitions (InputSource inputsource, Resource Resource) throws Beandefinitionstoreexception {try {int Validationmode = Getvalidationmodeforresource (Resource);D ocument doc = this.documentLoader.loadDocument (inputsource , Getentityresolver (), This.errorhandler, Validationmode, Isnamespaceaware ()); return Registerbeandefinitions (Doc, Resource);} catch (Beandefinitionstoreexception ex) {throw ex;} catch (Saxparseexception ex) {throw new Xmlbeandefinitionstoreexception (Resource.getdescription (), "line" + Ex.getlinenumber () + "in XML document from" + Resource + "is invalid", ex);} catch (Saxexception ex) {throw new Xmlbeandefinitionstoreexception (Resource.getdescription (),"XML document from" + Resource + "is invalid", ex);} catch (Parserconfigurationexception ex) {throw new Beandefinitionstoreexception (Resource.getdescription (), "Parser Configuration exception parsing XML from "+ Resource, ex);} catch (IOException ex) {throw new Beandefinitionstoreexception (Resource.getdescription (), "IOException parsing XML Document from "+ Resource, ex);} catch (Throwable ex) {throw new Beandefinitionstoreexception (Resource.getdescription (), "unexpected exception parsing XML document from "+ Resource, ex);}}

The code is not two lines, it is very unusual, it is estimated that when the document object is converted, it also contains some validation of the format of documents by default.


Come here first morning!









Spring source parsing--starting from Xmlbeanfactory's constructor function to see Loadbeandefinitions

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.