SpringBoot @Value @ConfigurationProperties

前言

在平時(shí)的工作中,我們可能會(huì)需要從配置文件***.properties***.yml中獲取一些配置參數(shù)。那么,在Spring Boot項(xiàng)目中我們可以使用@Value或者@ConfigurationProperties注解來(lái)獲取。接下來(lái),讓我們一起探索這兩個(gè)注解的詳細(xì)使用方法吧。

在接下來(lái)的內(nèi)容中,我們可以學(xué)習(xí)到如何在配置文件中配置 Key : Value、POJO、List<String>、List<POJO>、Map<String, String>Map<String, POJO> 結(jié)構(gòu),并在項(xiàng)目中使用 @Value@ConfigurationProperties 獲取這些配置。

正文

@Value 與 @ConfigurationProperties 的區(qū)別

spring-boot 官方文檔中,我們可以看到這段內(nèi)容:

大致內(nèi)容就是:

使用@Value("${property}")注解注入配置屬性有時(shí)會(huì)很麻煩,尤其是當(dāng)您使用多個(gè)屬性或您的數(shù)據(jù)本質(zhì)上是分層的時(shí)候。Spring Boot 提供了一種替代方法: @ConfigurationProperties,它讓強(qiáng)類型 bean 管理和驗(yàn)證應(yīng)用程序的配置項(xiàng)。

文檔中還總結(jié)了@ConfigurationProperties vs @Value 各自支持的功能特性,如下圖:

感興趣的朋友可以點(diǎn)擊上文中的鏈接去翻閱官方的詳細(xì)文檔,這里就不做過(guò)多介紹了。
言歸正傳,下面我們正式開(kāi)始列舉一些常用格式的配置項(xiàng)該如何使用@Value@ConfigurationProperties 去獲取。

1. 配置簡(jiǎn)單的 Key : Value 結(jié)構(gòu)

#### properties 文件配置格式:
app.environments-config.dev-url=https://dev.example.com

#### YML 文件配置格式:
app:
  environments-config:
    dev-url: https://dev.example.com

@Value 獲取配置

@Value("${app.environments-config.dev-url}")
private String devUrl;

@ConfigurationProperties 獲取配置
@Data
@Component
@ConfigurationProperties("app.environments-config")
public class Config {
    // 獲取配置
    private String devUrl;

}


2. 配置 List<String> 結(jié)構(gòu)

? 2.1 ? List<String> - 逗號(hào)分隔方式

?? 2.1.1 ? properties 文件配置格式:

?? 2.1.2 ? YML 文件配置格式:

從上圖中可以看出 @ConfigurationProperties 無(wú)需做任何操作就可以自動(dòng)將以逗號(hào)連接的配置項(xiàng)轉(zhuǎn)換為集合;而 @Value 則需要使用 SpEL 表達(dá)式來(lái)實(shí)現(xiàn)。至于為什么 @ConfigurationProperties 可自動(dòng)實(shí)現(xiàn)轉(zhuǎn)換,可以查閱官方文檔 Relaxed Binding 這部分內(nèi)容。

? 2.2 ? List<String> - 數(shù)組方式 (僅限于@ConfigurationProperties)

?? 2.2.1 ? properties 文件配置格式:

app.environments-config.dev-url[0]=https://dev.example.com
app.environments-config.dev-url[1]=https://dev222.example.com

?? 2.2.2 ? YML 文件配置格式:

app:
  environments-config:
    dev-url:
      - https://dev.example.com
      - https://dev222.example.com


3. 配置 POJO 結(jié)構(gòu) (僅限于@ConfigurationProperties)

? 3.1 ? properties 文件格式

? 3.2 ? YML 文件格式:


4. 配置 List<POJO> 結(jié)構(gòu) (僅限于@ConfigurationProperties)

? 4.1 ? properties 文件格式

? 4.2 ? YML 文件格式

\color{#376956}{溫馨提示:} 目錄 4.1 及 4.2 中不論是 properties文件格式還是 YML文件格式,其中配置的 dev-users 都可使用以逗號(hào)分隔的方式來(lái)編寫(xiě),會(huì)使配置文件顯得更加簡(jiǎn)潔。

5. 配置 Map<String, String> 結(jié)構(gòu)

? 5.1 ? @Value 對(duì)應(yīng)的寫(xiě)法

#### properties文件配置格式:
app.environments-config={"dev-url":"https://dev.example.com", "qa-url":"https://qa.example.com"} 

#### YML文件配置格式:
app:
  environments-config: '{"dev-url":"https://dev.example.com", "qa-url":"https://qa.example.com"}'

  // 獲取配置 
  @Value("#{${app.environments-config}}")
  private Map<String, String> value;

? 5.2 ? @ConfigurationProperties 對(duì)應(yīng)的寫(xiě)法

#### properties文件配置格式
app.environments-config.dev-url=https://dev.example.com
app.environments-config.qa-url=https://qa.example.com

#### YML文件配置格式
app:
  environments-config:
    dev-url: https://dev.example.com
    qa-url: https://qa.example.com

  @Data
  @Component
  @ConfigurationProperties("app")
  public class Config {
      // 獲取配置
      private Map<String, String> environmentsConfig;

  }


6. 配置 Map<String, POJO> 結(jié)構(gòu) (僅限于@ConfigurationProperties)

properties 文件配置方式:
#### 方式一:
app.environments-config.dev.url=https://dev.example.com
app.environments-config.dev.users=zhagnsan,lisi

app.environments-config.qa.url=https://qa.example.com
app.environments-config.qa.users=xiaobai,xiaohong
#### 方式二:
app.environments-config.dev.url=https://dev.example.com
app.environments-config.dev.users[0]=zhagnsan
app.environments-config.dev.users[1]=lisi

app.environments-config.qa.url=https://qa.example.com
app.environments-config.qa.users[0]=xiaobai
app.environments-config.qa.users[1]=xiaohong
YML 文件配置方式:
#### 方式一:
app:
  environments-config:
    dev:
      url: https://dev.example.com
      users: zhagnsan,lisi
    qa:
      url: https://qa.example.com
      users: xiaobai,xiaohong
#### 方式二:
app:
  environments-config:
    dev:
      url: https://dev.example.com
      users:
        - zhagnsan
        - lisi
    qa:
      url: https://qa.example.com
      users:
        - xiaobai
        - xiaohong
@ConfigurationProperties 獲取配置
@Data
@Component
@ConfigurationProperties("app")
public class Config {
    // 獲取配置
    private Map<String, EnvironmentsConfigBO> environmentsConfig;

    @Data
    public static class EnvironmentsConfigBO {
        private String url;
        private List<String> users;
    }

}

結(jié)語(yǔ):

通過(guò)上面文章的介紹,我們可以看出:在配置簡(jiǎn)單的 Key : Value 及 List<String> 結(jié)構(gòu)時(shí),可以使用 @Value 去獲取會(huì)更加方便,其他結(jié)構(gòu)如:POJO、List<POJO>、Map<String, String>、Map<String, POJO> 則使用 @ConfigurationProperties 去獲取。



擴(kuò)展閱讀:

@Value設(shè)置默認(rèn)值

// 冒號(hào)后面的內(nèi)容即為默認(rèn)值
@Value("${app.environments-config.dev-url:http://localhost}")
private String devUrl;


使用JSR303注解進(jìn)行有效性校驗(yàn)

一文學(xué)會(huì)JSR-303 參數(shù)校驗(yàn),真香


綁定屬性值到第三方j(luò)ar中包含的Class

如果需要綁定屬性值到第三方j(luò)ar中包含的Class對(duì)象,我們是無(wú)法直接在Class上加上@ConfigurationProperties注解的,這時(shí)候可以在@Configuration標(biāo)注的Class中定義一個(gè)需要綁定值的Class類型的bean,然后在該方法上加上@ConfigurationProperties。比如下面代碼中通過(guò)initTestConfigurationProperties()定義了一個(gè)TestConfigurationProperties類型的bean,在該方法上加上了@ConfigurationProperties,Spring Boot就會(huì)為該bean進(jìn)行屬性值綁定。

@Configuration
public class TestConfig {

    @Bean
    @ConfigurationProperties("test.config")
    public TestConfigurationProperties initTestConfigurationProperties() {
        return new TestConfigurationProperties();
    }
    
}



相關(guān)鏈接:

官方文檔
Spring Boot(07)——ConfigurationProperties介紹

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容