欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一个快速找到Spring框架是在哪里找到XML配置文件并解析Beans定义的小技巧 javaspringsapS/4HANACloud 

程序员文章站 2022-03-04 16:18:57
...

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框架是在哪里找到XML配置文件并解析Beans定义的小技巧
            
    
    
        javaspringsapS/4HANACloud 

 

The content of Beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--  http://*.com/questions/18802982/no-declaration-can-be-found-for-element-contextannotation-config
 -->
<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">   
   <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框架是在哪里找到XML配置文件并解析Beans定义的小技巧
            
    
    
        javaspringsapS/4HANACloud 

 

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框架是在哪里找到XML配置文件并解析Beans定义的小技巧
            
    
    
        javaspringsapS/4HANACloud 

 

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框架是在哪里找到XML配置文件并解析Beans定义的小技巧
            
    
    
        javaspringsapS/4HANACloud 

 

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

 

一个快速找到Spring框架是在哪里找到XML配置文件并解析Beans定义的小技巧
            
    
    
        javaspringsapS/4HANACloud 

 

My “helloWorld” bean is parsed here:

 

一个快速找到Spring框架是在哪里找到XML配置文件并解析Beans定义的小技巧
            
    
    
        javaspringsapS/4HANACloud 

 

 

一个快速找到Spring框架是在哪里找到XML配置文件并解析Beans定义的小技巧
            
    
    
        javaspringsapS/4HANACloud