使用@ConfigurationProperties實現(xiàn)類型安全的配置過程
@ConfigurationProperties實現(xiàn)類型安全的配置
問題描述
從之前@Value的使用,可以知道@Value可以靈活的把配置文件中的鍵值對的值注入到Bean中供我們使用,已經(jīng)很靈活了,但這還不夠,比如下述的application.properties
tomcat.ip=192.168.1.110 tomcat.port=8787 tomcat.projectName=screenshot tomcat.userName=admin tomcat.password=admin
如果也要按照之前的描述,使用@value就要填寫5次,這顯然令人惆悵,在程序員的世界里,所有重復(fù)性的工作都應(yīng)該被取代,因此Spring Boot為我們提供了@ConfigurationProperties注解。
實踐
application.properties
tomcat.ip=192.168.1.110 tomcat.port=8787 tomcat.projectName=screenshot
從上述的配置項中,可以看到明顯的相似性,即這一簇配置均以tomcat開始,類似String類中的startWith函數(shù)。
核心代碼
package com.wisely.ch6_2_3.config; import lombok.Getter; import lombok.Setter; import lombok.extern.log4j.Log4j; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "tomcat") //1 @Log4j public class TomcatSetting { @Getter @Setter private String ip; @Getter @Setter private int port; @Getter @Setter private String projectName; public String getUrl() { return "http://"+getIp()+"/"+getPort()+"/"+getProjectName(); } }
通過這種方法,即可實現(xiàn)一次性注入相似的配置,非常方便。
減少了重復(fù)性工作,即提高優(yōu)雅度,也能減少錯誤,很酷。
關(guān)于ConfigurationProperties注解的說明
下文筆者講述Spring Boot中ConfigurationProperties注解的相關(guān)說明
如下所示:
我們都知道在Spring中,可以使用@Value對單個屬性進行注入配置操作
但是很多配置都具有一定的層級,那么此時Spring提供了一個基于層級配置的注解
如下所示:
@ConfigurationProperties注解的功能:
- 將properties屬性和一個Bean及其屬性關(guān)聯(lián)
- 從而實現(xiàn)類型安全配置
例:
@ConfigurationProperties加載properties文件內(nèi)的配置 使用prefix屬性指定配置文件中定義的properties配置的統(tǒng)一前綴
@ConfigurationProperties(prefix = "remote"}) ?
---Spring Boot1.5之前,使用以下配置指定properties文件的位置
@ConfigurationProperties(prefix = "remote",locations={"classpath:remote.properties"})?
示例代碼如下:
remote.address= www.java265.com remote.port= 9090 ? @Component @PropertySource({"classpath:remote.properties"}) @ConfigurationProperties(prefix = "remote") public class RemoteConfig { ? ?? ? ? private String address; ? ? private int port; ? ? // getter/stetter方法 }
對應(yīng)RemoteConfig的Bean的使用:
@RestController public class ConfigurationController { ? ? @Resource ? ? private RemoteConfig remoteConfig; ? ? @GetMapping ? ? public void getInfo() { ? ? ? ? System.out.println("地址:" + remoteConfig.getAddress()); ? ? ? ? System.out.println("端口:" + remoteConfig.getPort()); ? ? } } //測試 @SpringBootTest @AutoConfigureMockMvc class ConfigurationControllerTest { ? ? @Autowired ? ? private MockMvc mockMvc; ? ? @Test ? ? void getInfo() throws Exception { ? ? ? ? mockMvc.perform(MockMvcRequestBuilders.get("/")); ? ? } } -----運行以上代碼,將輸出以下信息------ 地址:www.java265.com 端口:9090
例:
@ConfigurationProperties注解應(yīng)用于Bean方法上的示例分享
例:
@Configuration public class MyConfig { ? ? @Bean ? ? @ConfigurationProperties(prefix = "user") ? ? public User user() { ? ? ? ? return new User(); ? ? } }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于SpringBoot實現(xiàn)發(fā)送帶附件的郵件
這篇文章主要介紹了基于SpringBoot實現(xiàn)發(fā)送帶附件的郵件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11超簡單的java獲取鼠標點擊位置坐標的實例(鼠標在Jframe上的坐標)
在Java窗體Jframe上獲取鼠標點擊的坐標,其中使用了匿名內(nèi)部類,實例代碼非常簡單易懂,大家可以學(xué)習(xí)一下2018-03-03mybatis的association傳遞參數(shù)問題示例
這篇文章主要介紹了mybatis的association傳遞參數(shù)問題,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12java實現(xiàn)TCP socket和UDP socket的實例
這篇文章主要介紹了本文主要介紹了java實現(xiàn)TCP socket和UDP socket的實例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02