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";
    ....
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章