SpringBoot注解讲解(@ConfigurationProperties)

本文详细介绍了SpringBoot中@ConfigurationProperties注解的使用,包括如何将其与@Component结合、自定义配置文件及不同场景下的选择,帮助开发者理解和实现配置文件与JavaBean的自动绑定。
摘要由CSDN通过智能技术生成

1、@ConfigurationProperties的作用

SpringBoot提供的@ConfigurationProperties具有很强大的功能,该注解可以将properties配置文件中的内容读取并封装到JavaBean中,即配置绑定

使用前提!!!

只有在容器中的组件,才能使用SpringBoot提供的强大功能,也就是说将配置文件内容封装到某个类时,该类必须在容器内。

2、@Component+@ConfigurationProperties实现配置绑定

(1)在工程中新创建一个汽车类Car

package com.yjh.bean;
 
public class Car {
    
    private String name;
    private Integer price;
 
    public Car() {
    }
 
    public Car(String name, Integer price) {
        this.name = name;
        this.price = price;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Integer getPrice() {
        return price;
    }
 
    public void setPrice(Integer price) {
        this.price = price;
    }
 
    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

(2)在配置文件application.properties中添加内容

mycar.name = byd
mycar.price = 100000

(3)在Car上添加注解@Component将该组件加入容器,添加注解@ConfigurationProperties(prefix="mycar")其中参数prefix指定配置文件中的前缀。指定为mycar后会将配置文件中以mycar开头的内容赋值给组件中的属性。配置文件中的brand会赋值给组件的brand属性,price会赋值给组件的price属性。因此,必须保证配置文件前缀后的字段与组件属性对应。

package com.yjh.bean;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
 
    private String name;
    private Integer price;
 
    public Car() {
    }
 
    public Car(String name, Integer price) {
        this.name = name;
        this.price = price;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Integer getPrice() {
        return price;
    }
 
    public void setPrice(Integer price) {
        this.price = price;
    }
 
    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

此时并没有完成,IDEA会给一个提示,截图如下:

 其实不用管这个提示代码也可以运行。根据这个提示进入官网可以发现官网建议在配置文件中使用自定义元数据时,最好添加spring-boot-configuration-processor依赖,添加了这个依赖,我们在配置文件中自定义时编辑器会给我们提示。具体的使用方法就是在pom.xml中添加如下的dependency。

 (4)编写代码测试,在HelloWordController中自动注入容器中的car。

package com.yjh.Controller;
 
import com.yjh.bean.Car;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
 
@RestController
public class HelloWordController {
 
    @Autowired
    Car car;
 
    @RequestMapping("/Car")
    public Car getCar()
    {
        return car;
    }
 
    @RequestMapping("/Hello")
    public String handle01()
    {
        return "Hello Spring Boot 2";
    }
 
}


重新运行主程序,并访问http://localhost:8888/Car

 可以看到配置文件的数据已经绑定到容器中的组件。

3、@EnableConfigurationProperties + @ConfigurationProperties实现配置绑定

 (1)在配置类上加上注解@EnableConfigurationProperties(所要配置的类名.class)

 (2)在需要配置绑定的类上添加@ConfigurationProperties(prefis="前缀名")此时可以不用注解@Component,这是因为@EnableConfigurationProperties注解已经把对应的组件装入容器了。

 (3)进行测试

 测试成功。

4、总结

无论是@EnableConfigurationProperties + @ConfigurationProperties还是@Component+@ConfigurationProperties都能实现配置绑定。但是这两种方式在使用场景上有点区别。

(1)如果使用的第三方包中的组件,这时我们无法在该组件上添加@Component,此时使用@EnableConfigurationProperties + @ConfigurationProperties实现配置绑定

(2)在我们自定义的组件中,我个人觉得@Component+@ConfigurationProperties使用更便捷一点。

  • 24
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值