spring boot @ConfigurationProperties vs @Value

在spring boot的使用中,通过@ConfigurationProperties 和 @Value 两个注解可以给类赋值,但是两个的使用方式还是有些不同的,以下是官方说明以及自己在使用中的简介。

spring boot @ConfigurationProperties vs  @Value

1.根据他们的比较你可以简单的理解 :

  • 1 松绑定:

    @ConfigurationProperties使用中不必每个字都和配置中的key一致;@Value必须要一致 【可以理解成@ConfigurationProperties能够批量注入配置文件的属性,@Value只能一个个指定】

  • 2 数据源支持 :

    @ConfigurationProperties 还支持spring元数据,可以在resource下建立META-INF文件夹,然后建立文件additional-spring-configuration-metadata.json。里面的数据格式必须满足spring的元数据格式http://docs.spring.io/spring-boot/docs/1.5.3.RELEASE/reference/htmlsingle/#configuration-metadata

  • 3 Spring表达式语言(SpEL):

    @Value支持SqEL,简单理解必须单个参数赋值,即强绑定。

  • 4 @ConfigurationProperties还支持JSR303进行配置文件值及校验【个人新增,官网没体现】

2.实操说明

2.1Relaxed binding 松绑定

a.yml配置文件

#自定义 - 数据源配置 
myconfig.dbconfig: 
  needUpdateScheme: true
  dataUpdateType: prod #数据初始化类型 prod:只更新prod类型的init数据,test:更新所有类型的数据
  databases:   
    db0: 
      url: jdbc:mysql://172.168.0.31:3306/xxx?useSSL=false&useUnicode=true&characterEncoding=utf-8
      username: xxxx
      password: xxxx

b RepositoryProperties类为配置文件的对象类,DatabaseProperty为databases的对象类

 @ConfigurationProperties(prefix = "myconfig.dbconfig")
    public class RepositoryProperties {
    //是否需要更新数据库结构
    private String needUpdateScheme = "false";
    //数据源
    private Map<String, DatabaseProperty> databases = new HashMap<>();
    //数据初始化类型 prod:只更新prod类型的init数据,test:更新所有类型的数据
    private String dataUpdateType = "prod";

    public String getDataUpdateType() {
        return dataUpdateType;
    }

    public Map<String, DatabaseProperty> getDatabases() {
        return databases;
    }

    public String getNeedUpdateScheme() {
        return needUpdateScheme;
    }

    public void setNeedUpdateScheme(String needUpdateScheme) {
        this.needUpdateScheme = needUpdateScheme;
    }

    public void setDatabases(Map<String, DatabaseProperty> databases) {
        this.databases = databases;
    }

    public void setDataUpdateType(String dataUpdateType) {
        this.dataUpdateType = dataUpdateType;
    }

}
 public class DatabaseProperty {
    //连接串
    @NotNull  // --- 
    private String url;
    //数据库帐号
    private String username;
    //密码
    private String password;
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
} 

2.2 Meta-data 元数据,Spring Boot jar 包含元数据文件,提供所有支持的配置属性的详细信息。这些文件旨在允许IDE开发人员在用户使用application.properties 或application.yml文件时提供上下文帮助和“代码完成” 。为了改善用户体验并进一步协助用户配置给定的属性,您可以提供额外的元数据,在resource下建立META-INF文件夹,然后建立文件additional-spring-configuration-metadata.json,格式规范参考http://docs.spring.io/spring-boot/docs/1.5.3.RELEASE/reference/htmlsingle/#configuration-metadata。 【可参考 https://blog.csdn.net/L_Sail/article/details/70342023 ,这边就不进一步深入,有兴趣的童鞋可以尝试,做一个自定义的Meta-data】

2.3 Spring表达式语言(SpEL)


    public class RepositoryProperties {
    //是否需要更新数据库结构
    @Value("${myconfig.dbconfig.needUpdateScheme}")   
    private String needUpdateScheme = "false";
    ......

2.4 @ConfigurationProperties还支持JSR303进行配置文件值及校验

@ConfigurationProperties(prefix = "myconfig.dbconfig")
public class RepositoryProperties {
    @NotNull  //JSR303进行配置文件值及校验
    private String needUpdateScheme = "false";
    ....
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章