Spring XmlBeanFactory 容器的基本實現

来源:https://www.cnblogs.com/Yee-Q/archive/2022/07/03/16440067.html
-Advertisement-
Play Games

容器的基本用法 熟悉 Spring 的朋友應該都很瞭解下段代碼: public void testBeanFactory() { BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beanFactoryTest.xml")); Te ...



容器的基本用法

熟悉 Spring 的朋友應該都很瞭解下段代碼:

public void testBeanFactory() {
	BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beanFactoryTest.xml"));
    TestBean testBean = bf.getBean("testBean");
}

一段簡單的通過容器獲取 Bean 的代碼,它所完成的功能無非就是以下幾點:

  • 讀取配置文件 beanFactoryTest.xml
  • 根據 beanFactoryTest.xml 中的配置找到對應類的配置,並實例化
  • 獲取實例化後的實例

接下來我們來分析這段代碼的實現原理


Spring 核心類介紹

在開始正式的源碼分析之前,有必要先瞭解 Spring 核心的兩個類

1. DefaultListableBeanFactory

XmlBeanFactory 繼承自 DefaultListableBeanFactory,DefaultListableBeanFactory 是整個 bean 載入的核心部分,是 Spring 註冊及載入 bean 的預設實現。XmlBeanFactory 與 DefaultListableBeanFactory 的不同之處就在於 XmlBeanFactory 使用了自定義的 XML 讀取器 XmlBeanDefinitionReader

2. XmlBeanDefinitionReader

在 XmlBeanDefinitionReader 中主要包含以下幾步處理:

  1. 通過繼承自 AbstractBeanDefinitionReader 的方法,使用 ResourceLoader 將資源文件路徑轉換為對應的 Resoure 文件
  2. 通過 DocumentLoader 對 Resource 文件進行轉換,將 Resource 文件轉換為 Document 文件
  3. 通過實現 BeanDefinitionDocumentReader 的 DefaultBeanDefinitionDocumentReader 類對 Document 進行解析,並使用 BeanDefinitionParserDelegate 對 Element 進行解析

容器基礎 XmlBeanFactory

接下來我們將深入分析以下代碼的功能實現

BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beanFactoryTest.xml"));

通過 XmlBeanFactory 初始化時序圖,我們來看一看上面代碼的執行邏輯

1. 封裝配置文件

Spring 的配置文件讀取是通過 ClassPathResource 封裝成 Resource,Resource 的結構如下:

public interface Resource extends InputStreamSource {
    boolean exists();
    default boolean isReadable() {
        return this.exists();
    }
    default boolean isOpen() {
        return false;
    }
    default boolean isFile() {
        return false;
    }
    URL getURL() throws IOException;
    URI getURI() throws IOException;
    File getFile() throws IOException;
    default ReadableByteChannel readableChannel() throws IOException {
        return Channels.newChannel(this.getInputStream());
    }
    long contentLength() throws IOException;
    long lastModified() throws IOException;
    Resource createRelative(String var1) throws IOException;
    @Nullable
    String getFilename();
    String getDescription();
}

Resource 介面抽象了所有 Spring 內部使用的底層資源:File、URL、Classpath 等等,並定義了有關資源操作的方法。對於不同來源的資源文件,都有對應的 Resource 實現:文件(FileSystemResource)、Classpath(ClasspathResource)、URL(UrlResource )、InputStream(InputStreamResource)、Byte(ByteResource)等等,有了 Resource 介面就可以對所有資源文件進行統一處理,至於處理的實現其實很簡單,以 ClasspathResource 為例,實現方式就是通過 class 或者 classLoader 提供的底層方式進行調用

2. 數據準備階段

通過 Resource 完成配置文件的封裝以後,就將 Resource 作為 XmlBeanFactory 的構造函數參數傳入,代碼如下:

public XmlBeanFactory(Resource resource) throws BeansException {
    this(resource, (BeanFactory)null);
}

構造函數內部再次調用內部構造函數:

public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
    super(parentBeanFactory);
    this.reader = new XmlBeanDefinitionReader(this);
    this.reader.loadBeanDefinitions(resource);
}

this.reader.loadBeanDefinitions(resource); 是整個資源載入的切入點,這個方法的處理過程如下:

  1. 對參數 Resource 使用 EncodedResource 類進行封裝
  2. 從 Resource 獲取對應的 InputStream 並構造 InputSource
  3. 通過構造的 InputSource 實例和 Resource 實例繼續調用函數 doLoadBeanDefinitions

我們來看一下 loadBeanDefinitions 函數具體的實現過程:

public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
    return this.loadBeanDefinitions(new EncodedResource(resource));
}

EncodedResource 的作用是對資源文件的編碼進行處理,可以通過設置編碼屬性指定 Spring 使用響應的編碼進行處理

當構造好 EncodedResource 對象後,再次轉入到 loadBeanDefinitions(new EncodedResource(resource));

public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
    Assert.notNull(encodedResource, "EncodedResource must not be null");
    if (this.logger.isTraceEnabled()) {
        this.logger.trace("Loading XML bean definitions from " + encodedResource);
    }
	// 獲取已經載入的資源
    Set<EncodedResource> currentResources = (Set)this.resourcesCurrentlyBeingLoaded.get();
    if (!currentResources.add(encodedResource)) {
        throw new BeanDefinitionStoreException("Detected cyclic loading of " + encodedResource + " - check your import definitions!");
    } else {
        int var6;
        try {
            // 從已經封裝的 Resource 對象獲取 InputStream
            InputStream inputStream = encodedResource.getResource().getInputStream();
            Throwable var4 = null;

            try {
                InputSource inputSource = new InputSource(inputStream);
                if (encodedResource.getEncoding() != null) {
                    inputSource.setEncoding(encodedResource.getEncoding());
                }
				// 進入真正的邏輯核心部分
                var6 = this.doLoadBeanDefinitions(inputSource, encodedResource.getResource());
            } catch (Throwable var24) {
                var4 = var24;
                throw var24;
            } finally {
                if (inputStream != null) {
                    if (var4 != null) {
                        try {
                            inputStream.close();
                        } catch (Throwable var23) {
                            var4.addSuppressed(var23);
                        }
                    } else {
                        inputStream.close();
                    }
                }

            }
        } catch (IOException var26) {
            throw new BeanDefinitionStoreException("IOException parsing XML document from " + encodedResource.getResource(), var26);
        } finally {
            currentResources.remove(encodedResource);
            if (currentResources.isEmpty()) {
                this.resourcesCurrentlyBeingLoaded.remove();
            }

        }

        return var6;
    }
}

再次整理數據準備階段的邏輯,首先對傳入的 Resource 參數進行編碼處理,將準備的數據傳入到真正的核心處理部分 doLoadBeanDefinitions 方法

3. 獲取 Document

doLoadBeanDefinitions 方法的代碼如下:

protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource) throws BeanDefinitionStoreException {
    try {
        Document doc = this.doLoadDocument(inputSource, resource);
        int count = this.registerBeanDefinitions(doc, resource);
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Loaded " + count + " bean definitions from " + resource);
        }

        return count;
    } catch (BeanDefinitionStoreException var5) {
        throw var5;
    } catch (SAXParseException var6) {
        throw new XmlBeanDefinitionStoreException(resource.getDescription(), "Line " + var6.getLineNumber() + " in XML document from " + resource + " is invalid", var6);
    } catch (SAXException var7) {
        throw new XmlBeanDefinitionStoreException(resource.getDescription(), "XML document from " + resource + " is invalid", var7);
    } catch (ParserConfigurationException var8) {
        throw new BeanDefinitionStoreException(resource.getDescription(), "Parser configuration exception parsing XML from " + resource, var8);
    } catch (IOException var9) {
        throw new BeanDefinitionStoreException(resource.getDescription(), "IOException parsing XML document from " + resource, var9);
    } catch (Throwable var10) {
        throw new BeanDefinitionStoreException(resource.getDescription(), "Unexpected exception parsing XML document from " + resource, var10);
    }
}

protected Document doLoadDocument(InputSource inputSource, Resource resource) throws Exception {
    return this.documentLoader.loadDocument(inputSource, this.getEntityResolver(), this.errorHandler, this.getValidationModeForResource(resource), this.isNamespaceAware());
}

不考慮處理異常的代碼,其實只做了三件事:

  1. 獲取對 XML 文件的驗證模式
  2. 載入 XML 文件,並得到對應的 Document
  3. 根據返回的 Document 註冊 Bean 信息

獲取 XML 驗證模式是為了保證 XML 文件的正確性,常用的驗證模式有 DTD 和 XSD 兩種。Spring 通過 getValidationModeForResource 方法獲取對應資源的驗證模式,這裡不再贅述

protected int getValidationModeForResource(Resource resource) {
    int validationModeToUse = this.getValidationMode();
    // 如果手動指定了驗證模式就使用指定的驗證模式
    if (validationModeToUse != 1) {
        return validationModeToUse;
    } else {
        // 如果未指定就使用自動檢測
        int detectedMode = this.detectValidationMode(resource);
        return detectedMode != 1 ? detectedMode : 3;
    }
}

XmlBeanDefinitionReader 將文檔讀取交由 DocumentLoader 去處理,DocumentLoader 是個介面,真正調用的是 DefaultDocumentLoader,解析代碼如下:

public Document loadDocument(InputSource inputSource, EntityResolver entityResolver, ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {
    DocumentBuilderFactory factory = this.createDocumentBuilderFactory(validationMode, namespaceAware);
    if (logger.isTraceEnabled()) {
        logger.trace("Using JAXP provider [" + factory.getClass().getName() + "]");
    }
    DocumentBuilder builder = this.createDocumentBuilder(factory, entityResolver, errorHandler);
    return builder.parse(inputSource);
}

對於這部分代碼沒有太多可以描述的,因為通過 SAX 解析 XML 文檔的套路大都相同,解析完成返回一個 Document 對象

4. 解析及註冊 BeanDefinitions

當程式擁有 Document 對象後,就會被引入下麵這個方法:

public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
    // 使用 DefaultBeanDefinitionDocumentReader 實例化 BeanDefinitionDocumentReader
    BeanDefinitionDocumentReader documentReader = this.createBeanDefinitionDocumentReader();
    // 記錄統計前 BeanDefinition 的載入個數
    int countBefore = this.getRegistry().getBeanDefinitionCount();
    // 載入及註冊 bean
    documentReader.registerBeanDefinitions(doc, this.createReaderContext(resource));
    // 記錄本次載入的 BeanDefinition 個數
    return this.getRegistry().getBeanDefinitionCount() - countBefore;
}

BeanDefinitionDocumentReader 是一個介面,通過 createBeanDefinitionDocumentReader 方法完成實例化,實際類型是 DefaultBeanDefinitionDocumentReader,registerBeanDefinitions 方法代碼如下:

public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
    this.readerContext = readerContext;
    this.doRegisterBeanDefinitions(doc.getDocumentElement());
}

getDocumentElement 方法的重要目的之一是提取 root,以便於再次將 root 作為參數繼續 BeanDefinition 的註冊

再次進入 doRegisterBeanDefinitions 方法:

protected void doRegisterBeanDefinitions(Element root) {
    // 專門處理解析
    BeanDefinitionParserDelegate parent = this.delegate;
    this.delegate = this.createDelegate(this.getReaderContext(), root, parent);
    // 處理 profile 屬性
    if (this.delegate.isDefaultNamespace(root)) {
        String profileSpec = root.getAttribute("profile");
        if (StringUtils.hasText(profileSpec)) {
            String[] specifiedProfiles = StringUtils.tokenizeToStringArray(profileSpec, ",; ");
            if (!this.getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
                if (this.logger.isDebugEnabled()) {
                    this.logger.debug("Skipped XML bean definition file due to specified profiles [" + profileSpec + "] not matching: " + this.getReaderContext().getResource());
                }

                return;
            }
        }
    }
	// 解析前處理,留給子類實現
    this.preProcessXml(root);
    this.parseBeanDefinitions(root, this.delegate);
    // 解析後處理,留給子類實現
    this.postProcessXml(root);
    this.delegate = parent;
}

這裡使用了模板方法設計模式,如果繼承自 DefaultBeanDefinitionDocumentReader 的子類需要在 Bean 解析前後做一些處理的話,可以重寫 preProcessXml 和 postProcessXml 方法

在註冊 Bean 的最開始是先對 profile 屬性解析,profile 屬性可用於在配置文件中部署兩套配置分別適用生產環境和開發環境,做到方便的的切換環境

處理完 profile 屬性以後就可以進行 XML 的讀取,跟蹤代碼進入 parseBeanDefinitions 方法

protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
    if (delegate.isDefaultNamespace(root)) {
        NodeList nl = root.getChildNodes();

        for(int i = 0; i < nl.getLength(); ++i) {
            Node node = nl.item(i);
            if (node instanceof Element) {
                Element ele = (Element)node;
                if (delegate.isDefaultNamespace(ele)) {
                    this.parseDefaultElement(ele, delegate);
                } else {
                    delegate.parseCustomElement(ele);
                }
            }
        }
    } else {
        delegate.parseCustomElement(root);
    }
}

根節點或者子節點是預設命名空間的話採用 parseDefaultElement 方法解析,否則使用 delegate.parseCustomElement 方法解析,而對於標簽的解析,我們放到下一篇文章作講解



您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 背景介紹 在程式中,主線程啟動一個子線程進行非同步計算,主線程是不阻塞繼續執行的,這點看起來是非常自然的,都已經選擇啟動子線程去非同步執行了,主線程如果是阻塞的話,那還不如主線程自己去執行不就好了。那會不會有一種場景,非同步線程執行的結果主線程是需要使用的,或者說主線程先做一些工作,然後需要確認子線程執行 ...
  • 本文介紹了Netty對各種IO模型的支持以及如何輕鬆切換各種IO模型。還花了大量的篇幅介紹Netty服務端的核心引擎主從Reactor線程組的創建過程。在這個過程中,我們還提到了Netty對各種細節進行的優化,展現了Netty對性能極致的追求。 ...
  • 記憶體分析器 (MAT) 1. 記憶體分析器 (MAT) 1.1 MAT介紹 MAT是Memory Analyzer tool的縮寫。指分析工具。 1.2 MAT作用 Eclipse Memory Analyzer 是一種快速且功能豐富的Java 堆分析器,可幫助您發現記憶體泄漏並減少記憶體消耗。 使用記憶體 ...
  • 編程中一直對這兩個概念不是很理解,在網上搜了很多資料大概描述的其實都很模糊,有時候還自相矛盾,很容易搞混,這裡說一下我對這兩個概念的理解。 首先看一下相關技術書籍對這兩個概念的描述,下麵分別是摘自《深入理解Java核心技術》和《Java併發程式設計中的》的內容。 摘自《深入理解Java核心技術》14 ...
  • 題目鏈接:P2680 [NOIP2015 提高組] 運輸計劃 - 洛谷 | 電腦科學教育新生態 (luogu.com.cn) 看了好長時間題解才終於懂的,有關lca和二分答案的題解解釋的不詳細,一時半會理解不過來,於是自己寫一篇解釋儘管解釋主要在代碼中,希望能對迷茫的小伙伴有幫助 解析(主要為二分 ...
  • 通常在業務體系中,都會或多或少的涉及到支付相關的功能;對於一些經驗欠缺同學來說,最緊張的就是面對這類支付結算的邏輯,因為流程中的任何細節問題,都可能引發對賬異常的情況;錯誤發生之後,再想去修複流程,花費的時間成本又是高昂的,還牽扯錯誤數據的調平問題,最終很可能引發亂賬算不清的結果,然後需要人工介入手... ...
  • ArrayList分析3 : 刪除元素 轉載請註明出處:https://www.cnblogs.com/funnyzpc/p/16421743.html 對於集合類刪除元素是常有的需求,非常常見;如果是慣常的刪除方式就沒有寫本篇博客的必要了,本篇博客不光分析刪除可能導致的問題,也會從源碼層面分析為何 ...
  • 原文地址: Kotlin學習快速入門(7)——擴展的妙用 - Stars-One的雜貨小窩 之前也模模糊糊地在用這個功能,也是十分方便,可以不用繼承,快速給某個類增加新的方法,本篇便是來講解下Kotlin中擴展這一概念的使用 說明 先解釋一下,擴展的說明,官方文檔上解釋: Kotlin 能夠擴展一個 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...