欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot配置元數(shù)據(jù)方法教程

 更新時間:2019年12月05日 09:45:23   投稿:yaominghui  
這篇文章主要介紹了Spring Boot配置元數(shù)據(jù)方法教程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

這篇文章主要介紹了Spring Boot配置元數(shù)據(jù)方法教程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

前言

在編寫 Spring Boot 應(yīng)用程序時,將配置屬性映射到 Java bean 上是非常有用的。但是,記錄這些屬性的最好方法是什么呢?

在本教程中,我們將探討 Spring Boot Configuration Processor 關(guān)聯(lián)的 JSON 元數(shù)據(jù)文件,該 JSON 文檔記錄每個屬性的含義、約束等。

配置元數(shù)據(jù)

作為開發(fā)人員,我們開發(fā)的大多數(shù)應(yīng)用程序在某種程度上必須是可配置的。但是在通常情況下,我們并不能夠真正的理解配置參數(shù)的作用,比如它有默認值,又或者是過時的,有時我們甚至不知道該屬性的存在。

為了幫助我們理清楚,Spring Boot 生成了配置元數(shù)據(jù)的 JSON 文件,為我們提供關(guān)于如何使用屬性的有用信息。所以,配置元數(shù)據(jù)是一個描述性文件,它包含與配置屬性交互所需的必要信息。

這個文件的真正好處是IDE也可以讀取它,從而為我們自動配置完成Spring屬性以及其他配置提示。

依賴

為了生成此配置元數(shù)據(jù),我們將使用 spring-boot-configuration-processor 的依賴.

因此,讓我們繼續(xù)將依賴項添加為可選依賴 :

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

這種依賴關(guān)系將為我們提供在構(gòu)建項目時調(diào)用的 Java 注解處理器。我們稍后會詳細討論這個問題。

為了防止 @ConfigurationProperties 不應(yīng)用于我們的項目使用的其他模塊,在 Maven 中添加依賴項為可選依賴 是最好的做法。

配置屬性示例

現(xiàn)在來研究處理器是怎么工作的,我們需要使用 Java bean 獲取在 Spring Boot 應(yīng)用程序中包含一些屬性:

@Configuration
@ConfigurationProperties(prefix = "database")
public class DatabaseProperties {
  
  public static class Server {
    private String ip;
    private int port;
    
    // standard getters and setters
  }

  private String username;
  private String password;
  private Server server;
  
  // standard getters and setters
}

要做到這一點,我們可以使用 @ConfigurationProperties 注解。配置處理器會掃描使用了此注解的類和方法,用來訪問配置參數(shù)并生成配置元數(shù)據(jù)。

讓我們將這些屬性添加到屬性文件中。在示例中,我們把文件命名為 databaseproperties-test.properties:

#Simple Properties
database.username=baeldung
database.password=password

我們還將添加一個測試,以確保我們都做對了:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AnnotationProcessorApplication.class)
@TestPropertySource("classpath:databaseproperties-test.properties")
public class DatabasePropertiesIntegrationTest {

  @Autowired
  private DatabaseProperties databaseProperties;

  @Test
  public void whenSimplePropertyQueriedThenReturnsPropertyValue() 
   throws Exception {
    Assert.assertEquals("Incorrectly bound Username property", 
     "baeldung", databaseProperties.getUsername());
    Assert.assertEquals("Incorrectly bound Password property", 
     "password", databaseProperties.getPassword());
  }
}

我們通過內(nèi)部類 Server 還添加了嵌套屬性 database.server.id 和 database.server.port 。我們應(yīng)該添加內(nèi)部類 Server 以及一個 server 的屬性并且生成他的 getter 和 setter 方法。

在我們的測試中,讓我們快速檢查一下,確保我們也可以成功地設(shè)置和讀取嵌套屬性:

@Test
public void whenNestedPropertyQueriedThenReturnsPropertyValue() 
 throws Exception {
  Assert.assertEquals("Incorrectly bound Server IP nested property",
   "127.0.0.1", databaseProperties.getServer().getIp());
  Assert.assertEquals("Incorrectly bound Server Port nested property", 
   3306, databaseProperties.getServer().getPort());
}

好了,現(xiàn)在我們準備使用處理器了。

生成配置元數(shù)據(jù)

我們在前面提到過,配置處理器生成一個文件 – 它是使用注解處理實現(xiàn)的。

所以,在項目編譯之后,我們將在目錄 target/classes/META-INF 下看到文件名為 spring-configuration-metadata.json 的文件:

{
 "groups": [
  {
   "name": "database",
   "type": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties",
   "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
  },
  {
   "name": "database.server",
   "type": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server",
   "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties",
   "sourceMethod": "getServer()"
  }
 ],
 "properties": [
  {
   "name": "database.password",
   "type": "java.lang.String",
   "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
  },
  {
   "name": "database.server.ip",
   "type": "java.lang.String",
   "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server"
  },
  {
   "name": "database.server.port",
   "type": "java.lang.Integer",
   "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server",
   "defaultValue": 0
  },
  {
   "name": "database.username",
   "type": "java.lang.String",
   "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
  }
 ],
 "hints": []
}

接下來,讓我們看看更改 Java bean 上的注解如何影響元數(shù)據(jù)。

關(guān)于配置元數(shù)據(jù)的其他信息

首先,讓我們將 JavaDoc 注釋添加到 Server 上.

第二,讓我們給出一個 database.server.port 字段的默認值并最后添加 @Min 和 @Max 注解:

public static class Server {

  /**
   * The IP of the database server
   */
  private String ip;

  /**
   * The Port of the database server.
   * The Default value is 443.
   * The allowed values are in the range 400-4000.
   */
  @Min(400)
  @Max(800)
  private int port = 443;

  // standard getters and setters
}

如果我們檢查 spring-configuration-metadata.json 文件,我們將看到這些額外的信息得到了反映:

{
 "groups": [
  {
   "name": "database",
   "type": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties",
   "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
  },
  {
   "name": "database.server",
   "type": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server",
   "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties",
   "sourceMethod": "getServer()"
  }
 ],
 "properties": [
  {
   "name": "database.password",
   "type": "java.lang.String",
   "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
  },
  {
   "name": "database.server.ip",
   "type": "java.lang.String",
   "description": "The IP of the database server",
   "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server"
  },
  {
   "name": "database.server.port",
   "type": "java.lang.Integer",
   "description": "The Port of the database server. The Default value is 443.
    The allowed values are in the range 400-4000",
   "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties$Server",
   "defaultValue": 443
  },
  {
   "name": "database.username",
   "type": "java.lang.String",
   "sourceType": "com.baeldung.autoconfiguration.annotationprocessor.DatabaseProperties"
  }
 ],
 "hints": []
}

我們可以找到 database.server.ip 和 database.server.port 屬性的不同之處。事實上,額外的信息是非常有幫助的。開發(fā)人員和 IDE 都更容易理解每個屬性的功能。

我們還應(yīng)該確保觸發(fā)構(gòu)建以獲得更新的文件。在Eclipse中,如果選中“自動構(gòu)建”選項,則每個保存操作都會觸發(fā)一次構(gòu)建。在 IntelliJ 中,我們應(yīng)該手動觸發(fā)構(gòu)建。

理解元數(shù)據(jù)格式

讓我們仔細看看 JSON 元數(shù)據(jù)文件,并討論其組成。

Groups 是用于分組其他屬性的較高級別的項,而不指定值本身。在我們的例子中,我們有數(shù)據(jù)庫組,它也是配置屬性的前綴。我們還有一個 database 組,它是通過內(nèi)部類把 IP 和 port 屬性作為一個組。

屬性是可以為其指定值的配置項。這些屬性配置在后綴為 .properties或 .yml* 文件中,并且可以有額外的信息,比如默認值和驗證,就像我們在上面的示例中看到的那樣。

提示是幫助用戶設(shè)置屬性值的附加信息。例如,如果我們有一組屬性的允許值,我們可以提供每個屬性的描述。IDE 將為這些提示提供自動選擇的幫助。

配置元數(shù)據(jù)上的每個組成都有自己的屬性。來解釋配置屬性的詳細用法。

總結(jié)

在本文中,我們介紹了 Spring Boot 配置處理器及其創(chuàng)建配置元數(shù)據(jù)的功能。使用此元數(shù)據(jù)可以更輕松地與配置參數(shù)進行交互。

我們給出了一個生成的配置元數(shù)據(jù)的示例,并詳細解釋了它的格式和組成。

我們還看到了 IDE 上的自動完成支持是多么有幫助。

與往常一樣,本文中提到的所有代碼片段都可以在我們的 GitHub 存儲庫找到。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring動態(tài)添加定時任務(wù)的實現(xiàn)思路

    Spring動態(tài)添加定時任務(wù)的實現(xiàn)思路

    這篇文章主要介紹了Spring動態(tài)添加定時任務(wù)的實現(xiàn)思路,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-07-07
  • Spring AOP定義Before增加實戰(zhàn)案例詳解

    Spring AOP定義Before增加實戰(zhàn)案例詳解

    這篇文章主要介紹了Spring AOP定義Before增加,結(jié)合實例形式詳細分析了Spring面向切面AOP定義Before增加相關(guān)定義與使用技巧,需要的朋友可以參考下
    2020-01-01
  • 詳解Java中的Vector

    詳解Java中的Vector

    Vector 可實現(xiàn)自動增長的對象數(shù)組。本文通過實例代碼給大家詳細介紹java中的vector,感興趣的朋友一起看看吧
    2017-10-10
  • java stringbuffer的用法示例

    java stringbuffer的用法示例

    這篇文章主要介紹了java stringbuffer的用法示例,字符串緩沖區(qū),是一個容器(當返回到的是String時而且長度不確定,數(shù)據(jù)類型不確定時就可以用StringBuffer)其實底層還是數(shù)組,只是被封裝了,對外提供了方法,初始容量為16個字符
    2014-01-01
  • 詳解java的四舍五入與保留位示例

    詳解java的四舍五入與保留位示例

    本篇文章主要介紹了java的四舍五入與保留位示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Java基礎(chǔ)之Integer使用的注意事項及面試題

    Java基礎(chǔ)之Integer使用的注意事項及面試題

    這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)之Integer使用注意事項及面試題的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-12-12
  • java反編譯工具Bytecode-Viewer分享

    java反編譯工具Bytecode-Viewer分享

    這篇文章主要介紹了java反編譯工具Bytecode-Viewer分享,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-09-09
  • Java中List分片方式詳細解析

    Java中List分片方式詳細解析

    這篇文章主要介紹了Java中List分片方式詳細解析,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • java軟引用在瀏覽器使用實例講解

    java軟引用在瀏覽器使用實例講解

    在本篇文章里小編給大家整理的是一篇關(guān)于java軟引用在瀏覽器使用實例講解內(nèi)容,有興趣的朋友們可以學習下。
    2021-04-04
  • Spring?AOP實現(xiàn)聲明式事務(wù)機制源碼解析

    Spring?AOP實現(xiàn)聲明式事務(wù)機制源碼解析

    這篇文章主要為大家介紹了Spring?AOP實現(xiàn)聲明式事務(wù)機制源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12

最新評論