SpringBoot @ConfigurationProperties註解鬆散綁定錯誤

Caused by: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'foodProperties': Could not bind properties to 'FoodProperties' : prefix=configuration.food_properties, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.source.InvalidConfigurationPropertyNameException: Configuration property name 'configuration.food_properties' is not valid
	at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.bind

 Component 類配置如下,此處一開始與配置文件關聯的prefix爲駝峯命名(foodProperties),引發上面的錯誤;

@Component
@ConfigurationProperties(prefix = "configuration.foodProperties")
@Data
public class FoodProperties {
    private String name ;
}

配置文件:

configuration:
  foodProperties:
    name: MilkTea

經測試,得出與配置文件關聯的prefix不支持駝峯命名和蛇形命名(下劃線)。

將Component類和配置文件中的foodProperties改爲food,即可解決該錯誤。 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章