Spring配置文件解析之BeanDefinitionReader詳解
BeanDefinitionReader
Spring配置文件的解析是通過(guò)BeanDefinitionReader來(lái)實(shí)現(xiàn)的,其實(shí)了解BeanDefinitionReader實(shí)現(xiàn)的機(jī)制就會(huì)發(fā)現(xiàn),其只是將ApplicationContext.xml配置文件解析成Document對(duì)象,真正對(duì)xml中元素解析的類是在BeanDefinitionDocumentReader的實(shí)現(xiàn)類中來(lái)完成的,接下來(lái)我們也會(huì)對(duì)它進(jìn)行介紹。

對(duì)ApplicationContext.xml中開(kāi)始解析的方法是loadBeanDefinitions
AbstractBeanDefinitionReader的loadBeanDefinitions中會(huì)對(duì)ApplicationContext.xml進(jìn)行解析
@Override
public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {
Assert.notNull(locations, "Location array must not be null");
int counter = 0;
for (String location : locations) {
counter += loadBeanDefinitions(location);
}
return counter;
}@Override
public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
return loadBeanDefinitions(location, null);
}loadBeanDefinitions中會(huì)根據(jù)文件地址來(lái)進(jìn)行解析操作:
public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {
//判斷資源地址類型
if (resourceLoader instanceof ResourcePatternResolver) {
// Resource pattern matching available.
try {
Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
//在子類的loadBeanDefinitions進(jìn)行解析操作
int loadCount = loadBeanDefinitions(resources);
if (actualResources != null) {
for (Resource resource : resources) {
actualResources.add(resource);
}
}
if (logger.isDebugEnabled()) {
logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
}
return loadCount;
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
"Could not resolve bean definition resource pattern [" + location + "]", ex);
}
}
else {
// Can only load single resources by absolute URL.
Resource resource = resourceLoader.getResource(location);
//在子類的loadBeanDefinitions進(jìn)行解析操作
int loadCount = loadBeanDefinitions(resource);
if (actualResources != null) {
actualResources.add(resource);
}
if (logger.isDebugEnabled()) {
logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
}
return loadCount;
}
}loadBeanDefinitions是在子類XmlBeanDefinitionReader中實(shí)現(xiàn)
@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
return loadBeanDefinitions(new EncodedResource(resource));
}doLoadBeanDefinitions會(huì)將文件地址解析為數(shù)據(jù)流,然后解析數(shù)據(jù)流
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
..........
try {
//從xml文件中獲取流
InputStream inputStream = encodedResource.getResource().getInputStream();
try {
InputSource inputSource = new InputSource(inputStream);
if (encodedResource.getEncoding() != null) {
inputSource.setEncoding(encodedResource.getEncoding());
}
//解析文件流
return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
}
finally {
inputStream.close();
}
}
.......
}doLoadBeanDefinitions中會(huì)將文件流解析成Document對(duì)象
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
throws BeanDefinitionStoreException {
try {
Document doc = doLoadDocument(inputSource, resource);
return registerBeanDefinitions(doc, resource);
}
.....
}registerBeanDefinitions中會(huì)創(chuàng)建BeanDefinitionDocumentReader來(lái)對(duì)Document進(jìn)行解析,對(duì)Spring配置文件中的元素進(jìn)行解析處理。
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
//創(chuàng)建Document解析處理器
BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
int countBefore = getRegistry().getBeanDefinitionCount();
//在BeanDefinitionDocumentReader中解析xml中配置的元素
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
return getRegistry().getBeanDefinitionCount() - countBefore;
}總結(jié)
簡(jiǎn)單來(lái)說(shuō)BeanDefinitionReader所做的處理操作是將配置的ApplicationContext.xml解析成為Document對(duì)象,接下來(lái)會(huì)有 BeanDefinitionDocumentReader來(lái)對(duì)Document進(jìn)行解析。
到此這篇關(guān)于Spring配置文件解析之BeanDefinitionReader詳解的文章就介紹到這了,更多相關(guān)Spring的BeanDefinitionReader內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java基礎(chǔ)之多線程方法狀態(tài)和創(chuàng)建方法
Java中可以通過(guò)Thread類和Runnable接口來(lái)創(chuàng)建多個(gè)線程,下面這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)之多線程方法狀態(tài)和創(chuàng)建方法的相關(guān)資料,需要的朋友可以參考下2021-09-09
spring mvc+localResizeIMG實(shí)現(xiàn)HTML5端圖片壓縮上傳
這篇文章主要為大家詳細(xì)介紹了使用spring mvc+localResizeIMG實(shí)現(xiàn)HTML5端圖片壓縮上傳,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
基于SpringBoot服務(wù)端表單數(shù)據(jù)校驗(yàn)的實(shí)現(xiàn)方式
這篇文章主要介紹了基于SpringBoot服務(wù)端表單數(shù)據(jù)校驗(yàn)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
Java使用POI實(shí)現(xiàn)Excel文件的創(chuàng)建與處理
這篇文章主要為大家詳細(xì)介紹了Java如何采用POI原生方式實(shí)現(xiàn)自定義Excel表格表頭并生成Excel文件,文中的示例代碼講解詳細(xì),有需要的小伙伴可以了解下2025-05-05
Java遞歸實(shí)現(xiàn)JSON全路徑自動(dòng)化探測(cè)工具
在日常開(kāi)發(fā)中,面對(duì)嵌套十幾層、成百上千行的復(fù)雜 JSON,開(kāi)發(fā)者常面臨以下困境:路徑定位難,AI 幻覺(jué)問(wèn)題,文檔缺失等問(wèn)題,本文分享一個(gè)輕量級(jí)的 Java 工具類,利用遞歸算法一鍵拉平 JSON,輸出所有葉子節(jié)點(diǎn)的完整路徑,需要的朋友可以參考下2026-02-02
Java commons io包實(shí)現(xiàn)多線程同步圖片下載入門教程
這篇文章主要介紹了Java commons io包實(shí)現(xiàn)多線程同步圖片下載入門,commons io: 是針對(duì)開(kāi)發(fā)IO流功能的工具類庫(kù),其中包含了許多可調(diào)用的函數(shù),感興趣的朋友跟隨小編一起看看吧2021-04-04

