天天看點

ClassPathResource與PropertiesLoaderUtils

1.Springframeworks中的ClassPathResource類 用途:用來指定需要classLoader加載的資源(.properties/.yml) ClassPathResource resource = new ClassPathResource( 資源的位置 ) 2.Springframeworks裡的PropertiesLoaderUtils 類 用途:用來擷取資源檔案的内容 Properties properties = PropertiesLoaderUtils.loadProperties( ClassPathResource的實體類 ) 說明: Properties類 實作了Map接口,是以Properties類本質上是一種簡單的Map容器

Properties類與properties關系: Properties類本身表示了對一種Map結構的操作,properties檔案本身就表示了一個“鍵值對”的集合 (1)Properties類可以通過絕對/相對路徑方式加載properties檔案(讀取properties檔案) -->在spring架構中使用 ClassPathResource + PropertiesLoaderUtils -->普通情況下使用Properties類中的load()方法 => Properties類對象.load(InputStream inStream) (2)将程式中map集合通過Properties類儲存為properties檔案(儲存為properties檔案) -->使用Properties類中的store()方法 => properties.store(OutputStream out, String comments)