首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Spring Boot 讀取 application.yml 配置屬性完整實戰

     編輯:Java 相關知識

核心依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>  
</dependency>

此處理器能在編譯期生成 `META-INF/spring-configuration-metadata.json`,提供 IDE 智能提示與配置校驗。

YAML 結構化配置範例

myProps:
  simpleProp: simplePropValue
  arrayProps: 1,2,3,4,5
  listProp1:
    - name: abc
      value: abcValue
    - name: efg
      value: efgValue
  listProp2:
    - config2Value1
    - config2Vavlue2
  mapProps:
    key1: value1
    key2: value2

配置綁定 Bean

@Component
@ConfigurationProperties(prefix = "myProps")
public class MyProps {
    private String simpleProp;
    private String[] arrayProps;
    private List<Map<String, String>> listProp1 = new ArrayList<>();
    private List<String> listProp2 = new ArrayList<>();
    private Map<String, String> mapProps = new HashMap<>();

    // String 必須有 setter;集合/陣列/Map 僅需 getter(Spring 會實例化後填充)
    public String getSimpleProp() { return simpleProp; }
    public void setSimpleProp(String simpleProp) { this.simpleProp = simpleProp; }

    public String[] getArrayProps() { return arrayProps; }
    public void setArrayProps(String[] arrayProps) { this.arrayProps = arrayProps; }

    public List<Map<String, String>> getListProp1() { return listProp1; }
    public List<String> getListProp2() { return listProp2; }
    public Map<String, String> getMapProps() { return mapProps; }
    public void setMapProps(Map<String, String> mapProps) { this.mapProps = mapProps; }
}

單元測試驗證

@RunWith(SpringRunner.class)
@SpringBootTest
public class ConfigTest {
    @Autowired private MyProps myProps;
    @Autowired private ObjectMapper objectMapper;

    @Test
    public void propsTest() throws JsonProcessingException {
        System.out.println("simpleProp: " + myProps.getSimpleProp());
        System.out.println("arrayProps: " + objectMapper.writeValueAsString(myProps.getArrayProps()));
        System.out.println("listProp1: " + objectMapper.writeValueAsString(myProps.getListProp1()));
        System.out.println("listProp2: " + objectMapper.writeValueAsString(myProps.getListProp2()));
        System.out.println("mapProps: " + objectMapper.writeValueAsString(myProps.getMapProps()));
    }
}

測試輸出結果

simpleProp: simplePropValue
arrayProps: ["1","2","3","4","5"]
listProp1: [{"name":"abc","value":"abcValue"},{"name":"efg","value":"efgValue"}]
listProp2: ["config2Value1","config2Vavlue2"]
mapProps: {"key1":"value1","key2":"value2"}

關鍵細節總結

類型YAML 寫法Java 欄位Setter 需求
純量key: valueString/Integer 等必須
陣列key: 1,2,3String[] / List選填
列表-物件key: [{a:1},{a:2}]List<Map> / List<POJO>不需
列表-純量key: [v1, v2]List<String>不需
Mapkey: {k1:v1, k2:v2}Map<String, String>選填

進階技巧

  • 鬆綁綁定:`@ConfigurationProperties` 支援 `kebab-case`、`snake_case`、`camelCase` 自動映射
  • 嵌套前綴:`@ConfigurationProperties(prefix="app.datasource")` 可綁定 `app.datasource.url` 等
  • 驗證註解:在 Bean 欄位加 `@NotNull`、`@Size` 等 JSR-303 註解,啟用 `@Validated` 即可啟動配置校驗
  • 外部化覆蓋:啟動時 `--myProps.simpleProp=newValue` 即可覆蓋 YAML 值

掌握 `@ConfigurationProperties`,告別散落在程式碼中的 `@Value("${...}")`,擁抱型別安全、結構化、可驗證的配置管理方式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved