1. 程式人生 > 程式設計 >@ConfigurationProperties繫結配置資訊至Array、List、Map、Bean的實現

@ConfigurationProperties繫結配置資訊至Array、List、Map、Bean的實現

相關說明:

在SpringBoot中,我們可以通過以下幾種方式獲取並繫結配置檔案中的資訊:

  1. @Value註解。
  2. 使用Environment。
  3. @ConfigurationProperties註解。
  4. 通過實現ApplicationListener介面,註冊監聽器,進行硬編碼獲取,可參考:https://www.jb51.net/article/187407.htm
  5. 硬編碼載入檔案獲取。
  6. ……

注:一般情況下,第一種、第二種就夠用了;但是如果想直接從配置檔案中獲取到陣列、list、map、物件的話,
第一種和第二種的支援性不太好,目前只能獲取到陣列、list,對map、bean的話,就有點束手無策了。

這時我們可以使用第三種方式進行獲取。

環境說明:Windows10、IntelliJ IDEA、SpringBoot 2.1.5.RELEASE

@ConfigurationProperties註解獲取配置資訊並繫結到物件上示例:

準備工作:引入spring-boot-configuration-processor依賴

@ConfigurationProperties繫結配置資訊至Array、List、Map、Bean的實現

給出本人完整的pom.xml檔案:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.aspire.yaml-properties</groupId>
  <artifactId>yaml-properties</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>yaml檔案與properties檔案語法示例</name>
  <description>yaml檔案與properties檔案語法示例</description>
 
  <properties>
    <java.version>1.8</java.version>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
 
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
 
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
 
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
    </dependency>
  </dependencies>
 
 
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
 
</project>

載入demo.properties檔案中的資訊繫結到bean,並注入容器示例:

專案結構說明:

@ConfigurationProperties繫結配置資訊至Array、List、Map、Bean的實現

注:application.yml其實在本次演示中,沒有用到。不過本人給出瞭如何在yml檔案中進行類似配置的
示例(見本文末尾)。
追注:如果要讀取的是yml或yaml中的檔案,那麼最好是直接寫在application.yml或
application.yaml檔案中,寫在其他yml檔案中的話,可能導致讀取失敗。
(P.S.如果是寫在其他yml或yaml檔案中的話,只能通過最後一級key進行
定位,通過多級key可能定位不到。)

先給出一個等下會用到的Uer類:

@ConfigurationProperties繫結配置資訊至Array、List、Map、Bean的實現

注:上圖中的註解是快速開發lombok註解,也可以不使用這些註解,自己寫getter、setter等方法。

DemoConfig中是這樣的:

@ConfigurationProperties繫結配置資訊至Array、List、Map、Bean的實現

注:上圖中的@Data註解屬於快速開發lombok註解,該註解不是必須的,也可以不要該註解,自己手寫
getter、setter、toString等。

deme.properties中是這樣的:

@ConfigurationProperties繫結配置資訊至Array、List、Map、Bean的實現

測試一下:

@ConfigurationProperties繫結配置資訊至Array、List、Map、Bean的實現

執行測試方法,控制檯輸出(為了方便觀察,本人手動換行整理了一下輸出的內容):

@ConfigurationProperties繫結配置資訊至Array、List、Map、Bean的實現

由此可見,成功將demo.peoperties檔案中的資訊繫結到bean上,並注入Spring容器成功!

拓展:

如果是application.yml(或application.yaml)檔案,類似的,我們可以這麼配置:

@ConfigurationProperties繫結配置資訊至Array、List、Map、Bean的實現

參考連結
https://www.jb51.net/article/187407.htm

測試程式碼託管連結
https://github.com/JustryDeng/CommonRepository/tree/master

到此這篇關於@ConfigurationProperties繫結配置資訊至Array、List、Map、Bean的實現的文章就介紹到這了,更多相關@ConfigurationProperties繫結至Array、List、Map、Bean內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!