欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot @ConfigurationProperties注解松散绑定错误

程序员文章站 2024-01-08 12:06:22
...
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,即可解决该错误。