spring|Spring框架里解析配置文件的准确位置

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|Spring框架里解析配置文件的准确位置
文章图片

The content of Beans.xml:

【spring|Spring框架里解析配置文件的准确位置】

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|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|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|Spring框架里解析配置文件的准确位置
文章图片

from screenshot below we can find out the xml is parsed via SAX parser:
spring|Spring框架里解析配置文件的准确位置
文章图片

My “helloWorld” bean is parsed here:
spring|Spring框架里解析配置文件的准确位置
文章图片

要获取更多Jerry的原创文章,请关注公众号"汪子熙":
spring|Spring框架里解析配置文件的准确位置
文章图片

    推荐阅读