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,即可解決該錯誤。