ITPub博客

首页 > 应用开发 > Java > 一个快速找到Spring框架是在哪里找到XML配置文件并解析Beans定义的小技巧

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

原创 Java 作者:i042416 时间:2020-07-08 09:01:38 0 删除 编辑

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:



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,



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:



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 )



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



My “helloWorld” bean is parsed here:





要获取更多Jerry的原创文章,请关注公众号"汪子熙":


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24475491/viewspace-2703099/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
SAP成都研究院开发专家,SAP社区导师,SAP中国技术大使。

注册时间:2018-01-20

  • 博文量
    1914
  • 访问量
    882996