欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring配置文件解析之BeanDefinitionReader詳解

 更新時間:2024年02月14日 08:35:49   作者:軍偉@  
這篇文章主要介紹了Spring配置文件解析之BeanDefinitionReader詳解,ApplicationContext.xml配置文件解析成Document對象,真正對xml中元素解析的類是在BeanDefinitionDocumentReader的實(shí)現(xiàn)類中來完成的,需要的朋友可以參考下

BeanDefinitionReader

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

對ApplicationContext.xml中開始解析的方法是loadBeanDefinitions

AbstractBeanDefinitionReader的loadBeanDefinitions中會對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中會根據(jù)文件地址來進(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會將文件地址解析為數(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中會將文件流解析成Document對象

protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
			throws BeanDefinitionStoreException {
		try {
			Document doc = doLoadDocument(inputSource, resource);
			return registerBeanDefinitions(doc, resource);
		}
		.....
	}

registerBeanDefinitions中會創(chuàng)建BeanDefinitionDocumentReader來對Document進(jìn)行解析,對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é)

簡單來說BeanDefinitionReader所做的處理操作是將配置的ApplicationContext.xml解析成為Document對象,接下來會有 BeanDefinitionDocumentReader來對Document進(jìn)行解析。

到此這篇關(guān)于Spring配置文件解析之BeanDefinitionReader詳解的文章就介紹到這了,更多相關(guān)Spring的BeanDefinitionReader內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Swagger使用和注釋詳解

    Swagger使用和注釋詳解

    Swagger是一個規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù),這篇文章主要介紹了Swagger使用和注釋介紹,需要的朋友可以參考下
    2024-05-05
  • SpringBoot文件上傳(本地存儲)回顯前端操作方法

    SpringBoot文件上傳(本地存儲)回顯前端操作方法

    這篇文章主要介紹了SpringBoot文件上傳(本地存儲)回顯前端操作方法的相關(guān)資料,文中講解了文件上傳的基本原理,包括前端調(diào)用后端接口上傳文件,后端返回文件路徑給前端,前端通過路徑訪問圖片,需要的朋友可以參考下
    2024-11-11
  • 實(shí)例分析java開啟線程的方法

    實(shí)例分析java開啟線程的方法

    在本文里我們通過實(shí)例給大家講解了JAVA開啟線程的方法和相關(guān)知識點(diǎn),需要的朋友們跟著學(xué)習(xí)下。
    2019-03-03
  • Java static關(guān)鍵字詳細(xì)介紹與用法總結(jié)

    Java static關(guān)鍵字詳細(xì)介紹與用法總結(jié)

    這篇文章主要介紹了Java中static關(guān)鍵字的作用和用法詳細(xì)介紹,主要講了靜態(tài)方法、靜態(tài)變量、靜態(tài)類、static和final一塊用等內(nèi)容。需要的朋友可以參考下
    2017-04-04
  • mybatis-xml映射文件及mybatis動態(tài)sql詳解

    mybatis-xml映射文件及mybatis動態(tài)sql詳解

    XML映射文件的名稱與Mapper接口名稱一致,并且將XML映射文件和Mapper接口放置在相同包下(同包同名),這篇文章主要介紹了mybatis-xml映射文件及mybatis動態(tài)sql的相關(guān)知識,感興趣的朋友跟隨小編一起看看吧
    2024-12-12
  • 使用SpringBoot根據(jù)配置注入接口的不同實(shí)現(xiàn)類(代碼演示)

    使用SpringBoot根據(jù)配置注入接口的不同實(shí)現(xiàn)類(代碼演示)

    使用springboot開發(fā)時經(jīng)常用到@Autowired和@Resource進(jìn)行依賴注入,但是當(dāng)我們一個接口對應(yīng)多個不同的實(shí)現(xiàn)類的時候如果不進(jìn)行一下配置項(xiàng)目啟動時就會報錯,那么怎么根據(jù)不同的需求注入不同的類型呢,感興趣的朋友一起看看吧
    2022-06-06
  • Java中IO的NIO通道解析

    Java中IO的NIO通道解析

    這篇文章主要介紹了Java中IO的NIO通道解析,NIO 提供了與傳統(tǒng) BIO 模型中的 Socket 和 ServerSocket 相對應(yīng)的 SocketChannel 和 ServerSocketChannel 兩種不同的套接字通道實(shí)現(xiàn),需要的朋友可以參考下
    2024-01-01
  • 使用自定義參數(shù)解析器同一個參數(shù)支持多種Content-Type

    使用自定義參數(shù)解析器同一個參數(shù)支持多種Content-Type

    這篇文章主要介紹了使用自定義參數(shù)解析器同一個參數(shù)支持多種Content-Type的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 深入理解JVM之Java對象的創(chuàng)建、內(nèi)存布局、訪問定位詳解

    深入理解JVM之Java對象的創(chuàng)建、內(nèi)存布局、訪問定位詳解

    這篇文章主要介紹了深入理解JVM之Java對象的創(chuàng)建、內(nèi)存布局、訪問定位,結(jié)合實(shí)例形式詳細(xì)分析了Java對象的創(chuàng)建、內(nèi)存布局、訪問定位相關(guān)概念、原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-09-09
  • java HashMap通過value反查key的代碼示例

    java HashMap通過value反查key的代碼示例

    本文講解了java HashMap通過value反查key的方法,直接提供代碼供大家參考使用
    2013-11-11

最新評論