SpringBoot2底层注解@ConfigurationProperties配置绑定
作者:把苹果咬哭的测试笔记
SpringBoot2底层注解@ConfigurationProperties的配置绑定
我们通常会把一些经常变动的东西放到配置文件里。
比如之前写在配置文件application.properties里的端口号server.port=8080,另外常见的还有数据库的连接信息等等。
那么,我的数据库连接信息放在配置文件里,我要使用的话肯定得去解析配置文件,解析出的内容在 bean 里面去使用。
整个场景其实就是把配置文件里的所有配置,绑定到 java bean 里面。
要完成这个场景,基于 java 原生代码编写还是有点麻烦的。通常会做一个封装,读取到properties文件中的内容,并且把它封装到JavaBean中:
public class getProperties {
     public static void main(String[] args) throws FileNotFoundException, IOException {
         Properties pps = new Properties();
         pps.load(new FileInputStream("a.properties"));
         Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
         while(enum1.hasMoreElements()) {
             String strKey = (String) enum1.nextElement();
             String strValue = pps.getProperty(strKey);
             System.out.println(strKey + "=" + strValue);
             //封装到JavaBean
             ... ...
         }
     }这里就是使用Properties类来加载配置文件a.properties,然后遍历配置文件中的每一个k-v,获取之后就可以用到对应的地方。
在 springboot 中简化了这个过程,这就是配置绑定。
配置绑定
通过使用注解@ConfigurationProperties来完成配置绑定,注意需要结合@Component使用。
新建一个组件Car,有2个属性分别是品牌和价格:
@Component
public class Car {
    private String brand;
    private Integer price;
// get set tostring 就不贴了在配置文件application.properties,设置一些属性值,比如:
mycar.brand=QQmycar.price=9999
使用@ConfigurationProperties注解,加到组件上:
mycar.brand=QQ mycar.price=9999
传进去的 prefix 是配置文件里的前缀,这里就是 mycar。
验证
现在来测试一下是否绑定成功,在之前的HelloController继续增加一个控制器方法:
@RestController
public class HelloController {
    @Autowired
    Car car;
    @RequestMapping("/car")
    public Car car() {
        return car;
    }
    @RequestMapping("/hello")
    public String Hello() {
        return "Hello SpringBoot2 你好";
    }
}部署一下应用,浏览器访问http://localhost:8080/car:

绑定成功。
另一种方式
除上述方法之外,还可以使用@EnableConfigurationProperties + @ConfigurationProperties的方式来完成绑定。
注意,@EnableConfigurationProperties注解要使用在配置类上,表示开启属性配置的功能:
//@ConditionalOnBean(name = "pet1")
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = true)
@ImportResource("classpath:beans.xml")  //配置文件的类路径
@EnableConfigurationProperties(Car.class) //开启属性配置的功能
public class MyConfig {
 
    @Bean("user1")
    public User user01(){
        User pingguo = new User("pingguo",20);
        pingguo.setPet(tomcatPet());
        return pingguo;
    }
 
    @Bean("pet22")
    public Pet tomcatPet(){
        return new Pet("tomcat");
    }
}@EnableConfigurationProperties(Car.class)传入要开启配置的类,这可以自动的把 Car 注册到容器中,也就是说之前 Car 上的@Component就不需要了。
//@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
    private String brand;
    private Integer price;重新部署访问下地址,依然可以。

关于第二种的使用场景,比如这里的 Car 是一个第三方包里的类,但是人家源码没有加@Component注解,这时候就可以用这种方式进行绑定。
最后,要记住当使用@ConfigurationProperties(prefix = "mycar")这个配置绑定时,是跟 springboot 核心配置文件 application.properties文件的内容建立的绑定关系。
以上就是SpringBoot2底层注解@ConfigurationProperties配置绑定的详细内容,更多关于SpringBoot2注解配置绑定的资料请关注脚本之家其它相关文章!
您可能感兴趣的文章:
- SpringBoot中的@ConfigurationProperties注解解析
 - SpringBoot中@ConfigurationProperties注解的使用与源码详解
 - 关于SpringBoot的@ConfigurationProperties注解和松散绑定、数据校验
 - SpringBoot中@ConfigurationProperties注解实现配置绑定的三种方法
 - SpringBoot中注解@ConfigurationProperties与@Value的区别与使用详解
 - SpringBoot @ConfigurationProperties注解的简单使用
 - Springboot之@ConfigurationProperties注解解读
 
