The parsing process of Xml attribute configuration in Spring

1 Project overview

Insert picture description here

1.1 pom file

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.2.8.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
        <!-- 日志相关依赖 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.10</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.2</version>
        </dependency>
    </dependencies>

1.2 applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd"
       default-lazy-init="false">


    <bean id="student" class="com.rosh.bean.StudentBean"/>

    <context:component-scan base-package="com.rosh.service"/>

</beans>

1.3 StudentBean

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class StudentBean {
    
    


    private String name = "rosh";

    private String school = "杜桥中学";

    private String hobby = "篮球";

}

1.4 RoshTest

public class RoshTest {
    
    

    @Test
    public void mainTest(){
    
    
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentBean student = applicationContext.getBean(StudentBean.class);
        System.out.println(student.toString());

    }
}

1.5 Running results

Insert picture description here

2 Process analysis

2.1 Source code Debug

Description: call the refresh method in the construction method.
Insert picture description here
Description: call the AbstractApplicationContext refresh() method to parse the xml.
Insert picture description here
Insert picture description here
Insert picture description here
Emphasis: Create a parser for the XML object. Insert picture description here
Insert picture description here
Insert picture description here
Emphasis: Use the xml file parser to convert the xml file into a Reource object.Insert picture description here
Insert picture description here

Key point: Resource object to Document object Insert picture description here
**Key point: **Create Document parser Insert picture description here
Insert picture description here
Summary: DefaultBeanDefinitionDocumentReader (Document parser) parses xml filesInsert picture description here

2.2 Summary process

Approximate steps:
(1) Construction.
(2) Refresh the factory.
(3) Create an xml object parser.
(4) Parse xml into Resource objects.
(5) Resource to Document.
(6) Create a Document object parser.
(7) Parse the Document object.

detailed steps:

1 ClassPathXmlApplicationContext calls the constructor. 2 AbstractApplicationContext:
(1) Call the refresh() method. (2) Call the obtainFreshBeanFactory() method. 3
AbstractRefreshableApplicationContext calls the refreshBeanFactory() method. 4
AbstractXmlApplicationContext: (1)
loadBeanDefinitions(DefaultListableBeanFactory
beanFactory) method. Create an XML parser.
(2) Call the loadBeanDefinitions(XmlBeanDefinitionReader reader) method. 5 AbstractBeanDefinitionReader: (1) loadBeanDefinitions(String...
locations) method. (2) The loadBeanDefinitions(String location) method. (3) The
loadBeanDefinitions(String location, @Nullable Set
actualResources) method parses XML into Resource objects. (4) The
loadBeanDefinitions(Resource...resources) method. 6
XmlBeanDefinitionReader: (1) loadBeanDefinitions(Resource resource)
method. (2) The loadBeanDefinitions(EncodedResource encodedResource) method. (3)
doLoadBeanDefinitions(InputSource inputSource, Resource resource) method.
(4) The registerBeanDefinitions(Document doc, Resource
resource) method. Create a Document parser. 7 DefaultBeanDefinitionDocumentReader:
(1) registerBeanDefinitions(Document doc, XmlReaderContext readerContext) method.
(2) doRegisterBeanDefinitions(Element root) method.
(3) The parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) method parses the default tags and custom tags.

3 Parsing Bean tags

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

4 Custom label analysis

4.1 Step

  1. Get the namespace command space of the custom label, for example: http://www.springframework.org/schema/context
  2. Obtain the NamespaceHandler object according to the command space. Obtain the "META-INF/spring.handlers"
    files in all jar packages in spring through the SPI mechanism , and establish the mapping relationship
  3. Reflect to obtain the NamespaceHandler instance
  4. Call the init() method of the corresponding tag such as the ContextNameHandler tag
  5. Call the handler.parse() method to complete the analysis

4.2 Process analysis

1 Find the corresponding URL according to the custom tag
Insert picture description here
2 Load all MET-INF/spring.handlers in spring
Insert picture description here
Insert picture description here
3 Find the corresponding Hadnler according to applicationContext.xml
Insert picture description here

4.3 Debug

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_50654102/article/details/114784468