@ConfigurationProperties()注解
@ConfigurationProperties()注解的使用
- 当某个bean 中的属性值通过配置文件的方式绑定值时,可以使用@ConfigurationProperties()注解进行绑定,
例:将 brand 和price 属性值 绑定为 proberties 中的这个值时使用注解
可以通过两种方式使此注解生效 ,不管是那种必须先 将 bean注入到ioc容器才能使用
方法1: 在类中添加 @component 使该类注入容器,在使用@ConfigurationProperties(prefix = "mycar") 注解 属性值前缀前缀是一样的 mycar
@Component @ConfigurationProperties(prefix = "mycar") public class Car { private String brand; private Integer price; }
方法2: 在 组件类中 不用使用 通过@Component 注册到ioc,在springboo配置类中添加属性(可以在配置类或者启动类中添加),而是直接在配置类上添加注解 以下这个注解 @EnableConfigurationProperties(Car.class)
使用场景:当我们使用的这个类组件是第三方jar包的时候不同在源码上添加上@componet 所以这个这个注解排上用场
通过一个接口测试绑定的结果;
结果: