可以免费下载ppt模板的网站,怎么在别人网站做跳转,关键词优化工具有哪些,wordpress企业授权ConfigurationProperties注解主要用来把properties配置文件转化为bean来使用的#xff0c;而EnableConfigurationProperties注解的作用是ConfigurationProperties注解生效。如果只配置ConfigurationProperties注解#xff0c;在IOC容器中是获取不到properties配置文件转化的b…ConfigurationProperties注解主要用来把properties配置文件转化为bean来使用的而EnableConfigurationProperties注解的作用是ConfigurationProperties注解生效。如果只配置ConfigurationProperties注解在IOC容器中是获取不到properties配置文件转化的bean的
如果一个配置类只配置ConfigurationProperties注解而没有使用Component那么在IOC容器中是获取不到properties 配置文件转化的bean。说白了 EnableConfigurationProperties 相当于把使用 ConfigurationProperties 的类进行了一次注入。 测试发现 ConfigurationProperties 与 EnableConfigurationProperties 关系特别大。
1:写法一
1:写一个Car实体类
类上加上
Component
ConfigurationProperties(prefix mycar)
ConfigurationProperties注解可以把application.properties文件转化为bean使用Component注解把该bean注入到IOC容器中。
如果一个类只配置了 ConfigurationProperties 注解而没有使用 Component 注解将该类加入到 IOC 容器中那么它就不能完成 xxx.properties 配置文件和 Java Bean 的数据绑定
mycar是在如下application.properties中配置的 //只有在容器中的组件才会拥有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;}
}2:在application.properties配置
mycar.brandBBA
mycar.price100mycar就是Car实体类上
ConfigurationProperties(prefix mycar)的
brand和price就是属性名 3:写一个Controller类 RequestMapping(/web/)
RestController
public class CarController {Autowiredprivate Car car;PostMapping(/test)public Car test1(){return car;}
}4:用postman进行访问看到有返回能读取到配置文件中的值 2:写法二
1:把Component注解注视掉了表示不往容器中注册了 2:写一个
SpringConfig配置文件
在EnableConfigurationProperties中引入car类
EnableConfigurationProperties只是声明了属性绑定 3:再次访问有返回