(1)配置文件application.properties
mycar.brand=BYD
mycar.price=10000
(2)在中类添加@ConfigurationProperties注解
/**
* 只有在容器中的组件,才会拥有SpringBoot提供的@ConfigurationProperties注解的强大功能
*/
@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
@Configuration
@EnableConfigurationProperties(Car.class)
//1、开启Car配置绑定功能
//2、把这个Car这个组件自动注册到容器中
public class MyConfig {
}