Spring框架裡解析配置檔案的準確位置

i042416發表於2020-09-25

We can define bean configuration in xml and then can get instantiated bean instance with help of all kinds of containers for example ClassPathXmlApplicationContext as displayed below:


Spring框架裡解析配置檔案的準確位置


The content of Beans.xml:

<?xml version="1.0" encoding="UTF-8"?><!--  
    xmlns:xsi="
    xmlns:context="
    xsi:schemaLocation="
   <bean id="helloWorld" class="main.java.com.sap.HelloWorld">
       <property name="message" value="sss"/>
       <property name="testMin" value="2"/>
       <property name="phone" value="1"/>
   </bean></beans>

Where can we set breakpoint to start? No hint. Here is a tip: we can make the Beans.xml invalid by deliberately changing te tag bean to beana, and relaunch application. Now exception is raised as expected: Click the hyperlink XmlBeanDefinitionReader.java:399,


Spring框架裡解析配置檔案的準確位置


The line 399 where exception is raised will be automatically located. The core logic to load xml file is just near the exception raise position: line 391. So we can set breakpoint in line 391 now:


Spring框架裡解析配置檔案的準確位置


Change the tag from beana back to bean, and start application via debug mode. The code below is the core logic of Bean configuration file parse in Spring framework. The logic consists of two main steps:

(1) parse XML as a dom structure in memory ( line 391 ) (2) extract bean information contained in dom structure and generate BeanDefinition structure ( line 392 )


Spring框架裡解析配置檔案的準確位置


from screenshot below we can find out the xml is parsed via SAX parser:


Spring框架裡解析配置檔案的準確位置


My “helloWorld” bean is parsed here:


Spring框架裡解析配置檔案的準確位置 Spring框架裡解析配置檔案的準確位置


要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":

Spring框架裡解析配置檔案的準確位置


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2724092/,如需轉載,請註明出處,否則將追究法律責任。

相關文章