美文网首页
@ConfigurationProperties

@ConfigurationProperties

作者: 请叫我的全名cv工程师 | 来源:发表于2020-03-08 16:27 被阅读0次

使用的configurationProperties含义

application.yml 和 application.properties等方便了我们进行配置的管理和统一,但是在使用的是时候,我们还是通过@Value("${xxxx}")的形式去使用,例如

 @Value("${test.name}")
    private String name;
    @Value("${test.age}")
    private int age;
    @Value("${test.address}")
    private String address;

但是使用了@ConfigurationProperties配置之后,可以进一步简化@Value()的使用,最后的结果会变成这个简单

@Data
@Component
@ConfigurationProperties(prefix = "test")
public class TestConfigurationPropertyBean {
//    @Value("${test.name}")
    private String name;
//    @Value("${test.age}")
    private int age;
//    @Value("${test.address}")
    private String address;
}

可以不需要在pom.xml中增加依赖就可以使用了,反正我是没有加

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
    </dependency>

开始使用

1.在yml文件中写好配置参数

test:
  name: lirihong
  addres: nanshan
  age: 18
  1. 声明一个对象
@Data
@Component
//@EnableConfigurationProperties(TestConfigurationPropertyBean.class)
@ConfigurationProperties(prefix = "test")
public class TestConfigurationPropertyBean {
//    @Value("${test.name}")
    private String name;
//    @Value("${test.age}")
    private int age;
//    @Value("${test.address}")
    private String address;
}

添加@ConfigurationProperties注解在类的头部,并加上prefix的前缀,指定查找对应前缀test的配置
bean对象要有get 和set 的方法,所以使用了@Data注解
最后使用@Component 将对象都暴露成一个组件,交给spring去管理,方便其他得放的使用,不推荐使用@EnableConfigurationProperties(TestConfigurationPropertyBean.class)注解

3.编写测试类来进行测试

@SpringBootTest
class StudyApplicationTests {

    @Test
    void contextLoads() {
    }

    @Autowired
    TestConfigurationPropertyBean testConfigurationPropertyBean;
    @Test
    public void testConfigurationProperties() {
        System.out.println(testConfigurationPropertyBean.getAddress());
        System.out.println(testConfigurationPropertyBean.getAge());
        System.out.println(testConfigurationPropertyBean.getName());

        //System.out.println(testConfigurationPropertyBean.getChinese());
        //System.out.println(testConfigurationPropertyBean.getMath());
        //System.out.println(testConfigurationPropertyBean.getEnglish());
    }
}
  1. 输出结果


    image.png

基本完成



进阶
  1. 混合使用@ConfigurationProperties 和@Value
    有时候如果一个bean对象能尽量的内聚成只有和自己相关的参数属性,那是最好的,这样只需要使用一个@ConfigurationProperties就可以完成配置,但是总有一些情况,比如如果是对象是Controller,牵涉的和对象比较多,这个时候如果使用一个@ConfigurationProperties可能无法完成所有的配置注入,这个时候,还是可以使用原有的@Value配置进行注入值,从而达到效果
    application.yml
test:
  name: lirihong
  address: nanshan
  age: 18

score:
  Chinese: 98
  math: 100
  English: 99

bean

@Data
@Component
//@EnableConfigurationProperties(TestConfigurationPropertyBean.class)
@ConfigurationProperties(prefix = "test")
//@ConfigurationProperties(prefix = "score")
public class TestConfigurationPropertyBean {
//    @Value("${test.name}")
    private String name;
//    @Value("${test.age}")
    private int age;
//    @Value("${test.address}")
    private String address;

        @Value("${score.Chinese}")
    private int Chinese;
        @Value("${score.math}")
    private int math;
        @Value("${score.English}")
    private int English;
}

输出结果


image.png
  1. 加上参数校验@Validated
    在类的头部加上@Validated 注解,在字段上加上@NotEmpty等注解,在注入值的时候会自动校验值的参数,不符合则报错
    例如 application.yml


    image.png

    bean


    image.png

启动服务 就会报错,错误信息中会提示我们是name 子弹注入了空值,但是他是不允许为空的


image.png

3.未知的属性 ignoreUnknownFields
使用改注解可以尽量的查找出在yml中声明了,但是没有用的注解,这样可以简化和删除一些当初设定了但是后面没有用到的注解。默认ignoreUnknownFields = true,即使有多余的配置,也会忽略不管,但是将参数设置为false之后ignoreUnknownFields = false,服务启动的时候就会全部查找所有的yml 或者properties配置进行匹配,发现存在没有匹配的,都会报错
yml


image.png

bean


image.png
运行报错
image.png
  1. 慎用参数 ignoreInvalidFields
    如果我们在 application.yml属性上定义的属性不能被正确的解析会发生什么?假如我们为原本应该为布尔值的属性提供的值为 'pass':


    image.png

    但是需要的是一个boolean 的值


    image.png

默认情况下,Spring Boot 将会启动失败,并抛出异常:

image.png

如果将ignoreInvalidFields = true,那么程序启动的时候将会忽略这种转换错误的值,但是这样会给后续的程序排错带来更多不必要的麻烦,所以还是使用默认值false,程序启动的时候就会自动检验转换是否符合要求。

参考资料
@ConfigurationProperties 注解使用姿势,这一篇就够了

相关文章

网友评论

      本文标题:@ConfigurationProperties

      本文链接:https://www.haomeiwen.com/subject/uycldhtx.html