@ConfigurationProperties(prefix = "car") @Component public class Car { private String name; private Integer price; private Pet pet; // standard getters and setters }
@EnableConfigurationProperties开启对@ConfigurationProperties注解配置Bean的支持
也就是@EnableConfigurationProperties注解告诉Spring Boot 使能支持@ConfigurationProperties
配置类:
@Configuration @EnableConfigurationProperties(Car.class) public class MyConfig { @Bean("Tom") public Pet petBean() { return new Pet("cat"); } ...... }
普通类(需要读取配置文件)
@ConfigurationProperties(prefix = "car") public class Car { private String name; private Integer price; private Pet pet; // standard getters and setters }