一、SpringBoot 讀取 YML 配置文件
Spring Framework 是一個輕量級的IoC和AOP容器框架,SpringBoot 擴展了 Spring 的理念,並做出良好的封裝。它是針對 Spring 應用程序開發的框架,以便快速輕鬆地創建基於 Spring 的生產級別的應用程序。因為 SpringBoot 對 Spring 進行了封裝,所以我們不需要去配置大量繁瑣的 XML 文件。
在 SpringBoot 中,我們可以通過使用 @ConfigurationProperties 來綁定一個特定的屬性類,使得它可以自動讀取 application.yml 文件中的配置,使用方法如下:
# application.yml server: port: 8080 context-path: / servlet: context-path: / custom: field1: value1 field2: value2
import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties("custom") public class CustomProperties { private String field1; private String field2; // getter and setter methods }
在上述示例中,我們定義了一個名為 CustomProperties 的屬性類,並使用 @ConfigurationProperties(“custom”) 註解綁定了一個前綴為 custom 的屬性配置。這樣,我們直接在該類中定義需要使用的屬性字段,並通過 setter 方法自動完成綁定。
二、Jar 包讀取外部 YML 配置文件
有時候,在部署 jar 包的過程中,我們可能需要讀取 jar 包外部的 YML 配置文件。在 Java 中,可以使用 Properties 進行配置文件的讀取,但是 Properties 並不支持 YML 文件格式,所以我們需要引入相應的依賴。
org.yaml snakeyaml 1.27
在引入依賴之後,我們就可以通過如下代碼來讀取外部 YML 配置文件:
public class ExternalConfigReader { public static void main(String[] args) { InputStream inputStream = null; try { String filePath = "file:/opt/config/config.yml"; URL url = new URL(filePath); inputStream = url.openStream(); // 解析YML文件 Yaml yaml = new Yaml(); Map map = (Map) yaml.load(inputStream); // 輸出數據 System.out.println(map.get("field1")); System.out.println(map.get("field2")); } catch (Exception e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
在上述示例中,我們讀取了/opt/config/config.yml 文件,並使用 SnakeYaml 完成了對 YML 文件的讀取。接下來,我們可以通過 Map 對象獲取相應的屬性值。
三、如何讀取 YML 配置文件
除了 SpringBoot 和 Jar 包外部配置,我們還可以使用其他的方式來讀取 YML 配置文件。在 Java 中,通常會使用如下的兩種方式:
1. Properties 實現
Properties 是 Java I/O 中與關聯文件進行交互的類,主要用於讀取和寫入屬性文件。使用 Properties 讀取 YML 文件需要先將 YML 文件轉換成 Properties 格式,其格式與 YML 類似,只需要將 ‘:’ 替換為 ‘=’ 即可。
# config.yml field1: value1 field2: value2
// 讀取 YML 文件 Properties properties = new Properties(); InputStream inputStream = new FileInputStream("config.yml"); properties.load(new InputStreamReader(inputStream, "UTF-8")); // 輸出數據 System.out.println(properties.getProperty("field1")); System.out.println(properties.getProperty("field2"));
2. SnakeYaml 實現
SnakeYaml 是一款處理 YAML 格式文件的 Java 庫,可用於生成 Java 對象、解析 YAML 文件、綁定 Properties 等操作。使用 SnakeYaml,我們可以直接讀取 YML 配置文件並返回一個 Map 對象。
// 讀取 YML 文件 Yaml yaml = new Yaml(); InputStream inputStream = new FileInputStream("config.yml"); Map map = (Map) yaml.load(inputStream); // 輸出數據 System.out.println(map.get("field1")); System.out.println(map.get("field2"));
四、SpringBoot 讀取外部 YML 配置文件
在 SpringBoot 中,我們可以通過在 application.yml 文件中添加外部配置文件路徑來讀取外部 YML 配置文件:
# application.yml spring: profiles: active: prod config: location: file:/opt/config/ custom: field1: value1 field2: value2
在上述示例中,我們通過在 application.yml 中配置了外部配置文件的路徑 file:/opt/config/,SpringBoot 在啟動時會讀取該路徑下的配置文件。接下來,我們可以通過將該路徑下的配置文件轉換成一個屬性類來獲取相應的屬性值。具體示例可參考第一部分中的 SpringBoot 讀取 YML 配置文件部分。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/186604.html