日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

基于Xml 的IOC 容器-获得配置路径

發布時間:2024/4/13 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于Xml 的IOC 容器-获得配置路径 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

通過分析ClassPathXmlApplicationContext 的源代碼可以知道, 在創建ClassPathXmlApplicationContext 容器時,構造方法做以下兩項重要工作:

首先,調用父類容器的構造方法(super(parent)方法)為容器設置好Bean 資源加載器。

然后, 再調用父類AbstractRefreshableConfigApplicationContext 的setConfigLocations(configLocations)方法設置Bean 配置信息的定位路徑。

通過追蹤ClassPathXmlApplicationContext 的繼承體系, 發現其父類的父類AbstractApplicationContext 中初始化IOC 容器所做的主要源碼如下:

public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext {//靜態初始化塊,在整個容器創建過程中只執行一次static {//為了避免應用程序在Weblogic8.1 關閉時出現類加載異常加載問題,加載IOC 容//器關閉事件(ContextClosedEvent)類ContextClosedEvent.class.getName();}public AbstractApplicationContext() {this.resourcePatternResolver = getResourcePatternResolver();}public AbstractApplicationContext(@Nullable ApplicationContext parent) {this();setParent(parent);}//獲取一個Spring Source 的加載器用于讀入Spring Bean 配置信息protected ResourcePatternResolver getResourcePatternResolver() {//AbstractApplicationContext 繼承DefaultResourceLoader,因此也是一個資源加載器//Spring 資源加載器,其getResource(String location)方法用于載入資源return new PathMatchingResourcePatternResolver(this);}... }

AbstractApplicationContext 的默認構造方法中有調用PathMatchingResourcePatternResolver 的構造方法創建Spring 資源加載器:

public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) {Assert.notNull(resourceLoader, "ResourceLoader must not be null");//設置Spring的資源加載器this.resourceLoader = resourceLoader; }

在設置容器的資源加載器之后,接下來ClassPathXmlApplicationContext 執行setConfigLocations()方法通過調用其父類AbstractRefreshableConfigApplicationContext 的方法進行對Bean 配置信息的定位,該方法的源碼如下:

/*** Set the config locations for this application context in init-param style,* i.e. with distinct locations separated by commas, semicolons or whitespace.* <p>If not set, the implementation may use a default as appropriate.*/ //處理單個資源文件路徑為一個字符串的情況 public void setConfigLocation(String location) {//String CONFIG_LOCATION_DELIMITERS = ",; /t/n";//即多個資源文件路徑之間用” ,; \t\n”分隔,解析成數組形式setConfigLocations(StringUtils.tokenizeToStringArray(location, CONFIG_LOCATION_DELIMITERS)); }/*** Set the config locations for this application context.* <p>If not set, the implementation may use a default as appropriate.*/ //解析Bean定義資源文件的路徑,處理多個資源文件字符串數組 public void setConfigLocations(@Nullable String... locations) {if (locations != null) {Assert.noNullElements(locations, "Config locations must not be null");this.configLocations = new String[locations.length];for (int i = 0; i < locations.length; i++) {// resolvePath為同一個類中將字符串解析為路徑的方法this.configLocations[i] = resolvePath(locations[i]).trim();}}else {this.configLocations = null;} }

通過這兩個方法的源碼我們可以看出,我們既可以使用一個字符串來配置多個Spring Bean 配置信息,也可以使用字符串數組,即下面兩種方式都是可以的:

ClassPathResource res = new ClassPathResource("a.xml,b.xml");

多個資源文件路徑之間可以是用” , ; \t\n”等分隔。

ClassPathResource res =new ClassPathResource(new String[]{"a.xml","b.xml"});

至此,SpringIOC 容器在初始化時將配置的Bean 配置信息定位為Spring 封裝的Resource。

?

總結

以上是生活随笔為你收集整理的基于Xml 的IOC 容器-获得配置路径的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。