使用spring中抽象的資源載入配置檔案

2021-08-27 16:51:35 字數 629 閱讀 5907

熟悉spring的人都知道, spring把配置檔案等抽象為了resource,然後對resource做了擴充,包括檔案資源,網路資源等等。載入配置檔案有時候由於打包等原因導致讀取不到配置檔案等問題經常遇到,現在使用spring的resourceloader來解決這塊的問題。

1. 載入檔案:

使用filesystemresourceloader載入檔案;

2. 載入classpath檔案:

使用classpathresourceloader載入檔案(包括打包之後在jar包裡的配置檔案);

但是這樣很難記,spring做了路由, 直接使用defaultresourceloader就ok, **如下:

載入classpath下(包括jar包中的檔案)

@test

public void testclasspathpropertyfile() throws ioexception

}

載入檔案:只要把string locationpattern = "classpath:ognl/wei.properties";換成string locationpattern = "file:ognl/wei.properties";就正常了

spring外部資源抽象處理

resource 是spring對外部資源的抽象 public inte ce resource extends inputstreamsource boolean isopen uri geturi throws ioexception url geturl throws ioexception ...

Spring 中使用資源

在web應用中如果自已的程式裡面需要用到外部資源檔案 配置檔案 一般會把配置檔案放在classes下面或web inf下面。如果載入這結檔案的話通常有兩種辦法。一是寫絕對路徑,這是最不推薦的方法。二是引入servletcontext,從相對路徑中獲得絕對路徑。寫相對路徑的時候使用servletcon...

Spring中Resource(資源)的獲取

spring中獲取資源的方式一共有以下四種 下面是對每種方式的詳細講解 resource介面的實現類有 實現類說明 classpathresource 通過類路徑獲取資源檔案 filesystemresource 通過檔案系統獲取資源 urlresource 通過url位址獲取資源 bytearra...