@ConfigurationProperties注解用于自动配置绑定,可以将属性文件(application.properties)中的值注入到bean对象上,该注解使用必须将对象注入到IOC容器中才有配置绑定的功能。
方式1:
application.properties文件:@ConfigurationProperties+@Component ,@Component 注解用于将类中的对象注入到IOC容器中,@ConfigurationProperties用于将配置文件数据自动填充到bean中
mycar.brand=BYD
mycar.price=100000
/*
* 只有在容器中的组件才会有Springboot提供的强大功能
* */
@Component //将组件加到容器中
@ConfigurationProperties(prefix = "mycar")
public class Car {
private String brand;
private Integer price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
在配置类中,添加@EnableConfigurationProperties注解,表示开启属性配置功能,在后面添加需要开启属性配置功能的类

@EnableConfigurationProperties(Car.class) 实现了两个功能,1、开启Car配置绑定功能;2、把Car这个组件自动注入到容器中