今天就跟大家聊聊有關(guān)Spring Boot中怎么配置元數(shù)據(jù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)長期為千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為呼瑪企業(yè)提供專業(yè)的網(wǎng)站制作、成都網(wǎng)站建設(shè),呼瑪網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
為了生成此配置元數(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 注解處理器。我們稍后會詳細(xì)討論這個問題。
為了防止 @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)在我們準(zhǔn)備好來使用處理器了。
我們在前面提到過,配置處理器生成一個文件 – 它是使用注解處理實現(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ù)。
首先,讓我們將 JavaDoc 注釋添加到Server 上.
第二,讓我們給出一個 database.server.port 字段的默認(rèn)值并最后添加 @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中,如果我們檢查自動生成選項時,每個保存操作都將觸發(fā)生成。在 IntelliJ 中,我們應(yīng)該手動觸發(fā)構(gòu)建。
讓我們仔細(xì)看看 JSON 元數(shù)據(jù)文件,并討論其組成。
Groups 是用于分組其他屬性的較高級別的項,而不指定值本身。在我們的例子中,我們有數(shù)據(jù)庫組,它也是配置屬性的前綴。我們還有一個 database 組,它是通過內(nèi)部類把 IP 和 port 屬性作為一個組。
屬性是可以為其指定值的配置項。這些屬性配置在后綴為 *.properties或 .yml 文件中,并且可以有額外的信息,比如默認(rèn)值和驗證,就像我們在上面的示例中看到的那樣。
提示是幫助用戶設(shè)置屬性值的附加信息。例如,如果我們有一組屬性的允許值,我們可以提供每個屬性的描述。IDE 將為這些提示提供自動選擇的幫助。
看完上述內(nèi)容,你們對Spring Boot中怎么配置元數(shù)據(jù)有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
文章標(biāo)題:SpringBoot中怎么配置元數(shù)據(jù)
網(wǎng)站鏈接:http://jinyejixie.com/article24/gphjce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、App設(shè)計、電子商務(wù)、關(guān)鍵詞優(yōu)化、靜態(tài)網(wǎng)站、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)