1 Project overview
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
2 Process analysis
2.1 Source code Debug
Description: call the refresh method in the construction method.
Description: call the AbstractApplicationContext refresh() method to parse the xml.
Emphasis: Create a parser for the XML object.
Emphasis: Use the xml file parser to convert the xml file into a Reource object.
Key point: Resource object to Document object
**Key point: **Create Document parser
Summary: DefaultBeanDefinitionDocumentReader (Document parser) parses xml files
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
4 Custom label analysis
4.1 Step
- Get the namespace command space of the custom label, for example: http://www.springframework.org/schema/context
- 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- Reflect to obtain the NamespaceHandler instance
- Call the init() method of the corresponding tag such as the ContextNameHandler tag
- Call the handler.parse() method to complete the analysis
4.2 Process analysis
1 Find the corresponding URL according to the custom tag
2 Load all MET-INF/spring.handlers in spring
3 Find the corresponding Hadnler according to applicationContext.xml
4.3 Debug