基於Xml 的IOC 容器 獲得配置路徑

2021-10-10 13:11:39 字數 1134 閱讀 6366

首先,呼叫父類容器的構造方法(super(parent)方法)為容器設定好bean 資源載入器。

//靜態初始化塊,在整個容器建立過程中只執行一次

static

this.resourcepatternresolver = getresourcepatternresolver();

} this();

setparent(parent);

} //獲取乙個spring source 的載入器用於讀入spring bean 配置資訊

protected resourcepatternresolver getresourcepatternresolver()

...}

public pathmatchingresourcepatternresolver(resourceloader resourceloader)
/**

* i.e. with distinct locations separated by commas, semicolons or whitespace.

*///處理單個資源檔案路徑為乙個字串的情況

public void setconfiglocation(string location)

/** */

//解析bean定義資源檔案的路徑,處理多個資源檔案字串陣列

public void setconfiglocations(@nullable string... locations)

} else

}

通過這兩個方法的原始碼我們可以看出,我們既可以使用乙個字串來配置多個spring bean 配置資訊,也可以使用字串陣列,即下面兩種方式都是可以的:

classpathresource res = new classpathresource("a.xml,b.xml");
多個資源檔案路徑之間可以是用」 , ; \t\n」等分隔。

classpathresource res =new classpathresource(new string);
至此,springioc 容器在初始化時將配置的bean 配置資訊定位為spring 封裝的resource。

基於Xml 的IOC 容器 解析配置檔案路徑

獲取resource的具體實現方法 override public resource getresource string location 如果是類路徑的方式,那需要使用classpathresource 來得到bean 檔案的資源物件 if location.startswith else if...

基於Xml 的IOC 容器 分配解析策略

xmlbeandefinitionreader 類中的doloadbeandefinition 方法是從特定xml 檔案中實際載入bean 配置資源的方法,該方法在載入bean 配置資源之後將其轉換為document 物件,接下來呼叫registerbeandefinitions 啟動spring ...

基於Xml 的IOC 容器 載入 bean 元素

bean 配置資訊中的和元素解析在defaultbeandefinitiondocumentreader 中已經完成,對bean 配置資訊中使用最多的元素交由beandefinitionparserdelegate 來解析,其解析實現的原始碼如下 解析元素的入口 nullable public be...