- The core containers of spring include: core, beans, context, express language four modules. So for a simple spring project, the basic thing is to rely on the following three jar packages:
<Dependency> <groupId>Org.springframework</groupId> <Artifactid>Spring-core</Artifactid> <version>4.3.8.RELEASE</version> </Dependency> <Dependency> <groupId>Org.springframework</groupId> <Artifactid>Spring-context</Artifactid> <version>4.3.8.RELEASE</version> </Dependency> <Dependency> <groupId>Org.springframework</groupId> <Artifactid>Spring-beans</Artifactid> <version>4.3.8.RELEASE</version> </Dependency>
The Express language module is not considered for the time being.
- The approximate process of creating a spring bean from an XML file
- The main considerations are as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:xsi:schemaLocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <BeanID= "Mytestbean"class= "Chapter02." Mytestbean "/></Beans>
New Xmlbeanfactory (new classpathresource ("Beanfactorytest.xml"));
Note: Beanfactorytest.xml refers to the XML file above; Chapter02.mytestbean is a simple bean (contains a private property, Get,set method)
- About New Classpathresource ("Beanfactorytest.xml")
- The interface Inputstreamsource encapsulates any class that can return InputStream. It has only one method definition: getInputStream (); The method returns a InputStream object.
- The resource interface inherits the Inputstreamsource interface
- The Classpathresource class is an implementation class for the resource interface
- About New Xmlbeanfactory (new Classpathresource ("Beanfactorytest.xml"))
- First, the incoming resource parameters are encapsulated
- Second, prepare the InputSource object by using Sax to read the XML file.
- Finally, the prepared data is passed through the parameters into the true core processing section: Doloadbeandefinitions
- For the real core processing part: Doloadbeandefinitions
- Gets the validation mode for the XML file: DTD, XSD
- Load the XML file and get the corresponding document. The steps for parsing an XML document through Sax are as follows:
- First create the Documentbuilderfactory
- Create Documentbuilder with Documentbuilderfactory
- To return a Document object using Documentbuilder parsing InputSource
- Registering bean information based on the returned document
- The implementation class of the Beandefinitiondocumentreader interface Defaultbeandefinitiondocumentreader gets the root node of the Document object
- Get all the bean nodes through the root node: one is the default bean node: Use Parsedefaultelement (); Method for processing; one is a custom bean node: Using Delegate.parsecustomelement () method, where delegate is the custom node processor that is obtained from the namespace of the custom node, and the following discussion is only for the first case: processing of the default bean node.
- There are four types of tags for the default bean node: Import, alias, Bean, beans, which we mainly discuss the most commonly used and most complex bean tags:
- First, the parsebeandefinitionelement () of the Beandefinitiondelegate class is delegated, the method parses the element and returns an instance of the Beandefinitionholder type Bdholder
The ID in the
- extract element and the Name property
- Further parse all other attributes and child elements, and uniformly encapsulate into an instance of the Genericbeandefinition type Note: There are three implementation classes in Beandefinition: rootbeandefinition (parent node), Childbeandefinition (child node), Genericbeandefinition (the Java container for XML attributes)
- resolves various properties: The Scope property, the Singleton property, the abstract property, the Lazy-init property, the Autowire property, the Dependency-check property, the Depends-on property, Autowire-candidate Properties, primary properties, Init-method properties, Destory-method properties, Factory-method properties, Factory-bean properties,
- parse various child elements:
- Parsing metadata: parsemetaelements
- parsing lookup-method child elements: parselookupoverridesubelements
- parsing repl Aced-method child element: parsereplacedmethodsubelements
- Parse constructor parameter: parseconstructorargelements
- Parse Property child Element: parsepropertyelements
- parsing qualifier child elements: parsequalifierelements
- If the BE is detected An does not specify Beanname, then use the default rule to produce Beanname
- for this bean to encapsulate the obtained information into an instance of Beandefinitionholder
- when a custom attribute exists under the returned Bdholder, the custom label also needs to be parsed. For example, the following contains Mybean:user custom properties:
< bean Span style= "COLOR: #ff0000" >id = "test" Class = "test. Myclass " > < mybean:user username =" AAA " /> </ bean >
Need to use delegate.decoratebeandefinitionifrequired (); Handle the custom label as follows:
- traverse all properties and all child elements
- The namespace that gets the label based on the node
- determines whether it is a default label based on the namespace, only the non-default label is processed, because the default label has been processed before
- finds the corresponding processor from the namespace
- Retouch with the appropriate processor
- Registration of Bdholder After parsing is completed: including registration via Beanname and alias alias registration
- Registered with Beanname BeanDefinition:this.beanDefinitionMap.put (Beanname, beandefinition)
- Verify the Methodovirrides property of the Abstractbeandefinition
- If the Beanname has been registered: If the user has set not allow duplicate registration, then throw exception, otherwise directly overwrite, re-register
- Join Beandfinitionmap
- Clears the cache of the corresponding Beanname left before parsing
- Register BeanDefinition:this.aliasMap.put (alias, name) via alias (aliases);
- Alias is the same as beanname, you do not need to process and delete the original alias
- If the aliasname exists, the user is allowed to overwrite the alias to do the appropriate processing
- Alias Loop check. For example: when there is a (key)-->b (value), if there is also B (key)-->a (value) or B (key)-->c (value), C (key)-->a (value), it is called a loop occurs. An exception is thrown at this point
- Registered Alias:this.aliasMap.put (alias, name);
- Finally, a response event is issued to notify the relevant listener
Spring--bean the creation process