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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

基于Xml 的IOC 容器-解析配置文件路径

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

XmlBeanDefinitionReader 通過調用ClassPathXmlApplicationContext 的父類DefaultResourceLoader 的getResource()方法獲取要加載的資源,其源碼如下

//獲取Resource的具體實現方法 @Override public Resource getResource(String location) {Assert.notNull(location, "Location must not be null");for (ProtocolResolver protocolResolver : this.protocolResolvers) {Resource resource = protocolResolver.resolve(location, this);if (resource != null) {return resource;}}//如果是類路徑的方式,那需要使用ClassPathResource 來得到bean 文件的資源對象if (location.startsWith("/")) {return getResourceByPath(location);}else if (location.startsWith(CLASSPATH_URL_PREFIX)) {return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());}else {try {// Try to parse the location as a URL...// 如果是URL 方式,使用UrlResource 作為bean 文件的資源對象URL url = new URL(location);return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));}catch (MalformedURLException ex) {// No URL -> resolve as resource path.//如果既不是classpath標識,又不是URL標識的Resource定位,則調用//容器本身的getResourceByPath方法獲取Resourcereturn getResourceByPath(location);}} }

DefaultResourceLoader 提供了getResourceByPath()方法的實現,就是為了處理既不是classpath標識,又不是URL 標識的Resource 定位這種情況。

protected Resource getResourceByPath(String path) {return new ClassPathContextResource(path, getClassLoader()); }

在ClassPathResource 中完成了對整個路徑的解析。這樣,就可以從類路徑上對IOC 配置文件進行加載,當然我們可以按照這個邏輯從任何地方加載,在Spring 中我們看到它提供的各種資源抽象,比如ClassPathResource、URLResource、FileSystemResource 等來供我們使用。上面我們看到的是定位Resource 的一個過程,而這只是加載過程的一部分。例如FileSystemXmlApplication 容器就重寫了getResourceByPath()方法:

@Override protected Resource getResourceByPath(String path) {if (path.startsWith("/")) {path = path.substring(1);}//這里使用文件系統資源對象來定義bean 文件return new FileSystemResource(path); }

通過子類的覆蓋,巧妙地完成了將類路徑變為文件路徑的轉換。

?

總結

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

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