SpringBoot項(xiàng)目獲取統(tǒng)一前綴配置及獲取非確定名稱配置方法
SpringBoot項(xiàng)目獲取統(tǒng)一前綴配置以及獲取非確定名稱配置
在SpringBoot項(xiàng)目中,我們經(jīng)??吹浇y(tǒng)一前綴的配置,我們?cè)撛趺唇y(tǒng)一獲取
my.config.a.name=xiaoming
my.config.a.age=18
my.config.a.address=guangdongmy.config.b.name=xiaomli
my.config.b.age=20
my.config.b.address=shandong
方式一:使用對(duì)應(yīng)的配置類并結(jié)合注解:@ConfigurationProperties(prefix = “xxx.xxx”)
配置文件:
my.config.name=xiaoming my.config.age=18 my.config.address=guangdong
對(duì)應(yīng)的配置類:MyProperties
import lombok.Getter; import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @author gooluke */ @Component @ConfigurationProperties(prefix = "my.config") @Getter @Setter public class MyProperties { private String name; private int age; private String address; }
獲取配置類,打印屬性:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class TestController { @Autowired private MyProperties myProperties; @RequestMapping("/show") public void show() { System.out.println("myProperties.getName() = " + myProperties.getName()); System.out.println("myProperties.getAge() = " + myProperties.getAge()); System.out.println("myProperties.getAddress() = " + myProperties.getAddress()); } }
打印結(jié)果:
方式二:獲取統(tǒng)一前綴,而后面非確定字段名的配置
配置文件:
my.config.a.name=xiaoming my.config.a.age=18 my.config.a.address=guangdong my.config.b.name=xiaomli my.config.b.age=20 my.config.b.address=shandong
對(duì)應(yīng)的配置類:
import lombok.Getter; import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Map; /** * @author gooluke */ @Component @ConfigurationProperties(prefix = "my") @Getter @Setter public class MyProperties2 { //這里的config得對(duì)應(yīng)上my.config.xx里的config private Map<String, UserInfoConfig> config; @Setter @Getter public static class UserInfoConfig { private String name; private Integer age; private String address; } }
獲取配置類,打印屬性:
@Autowired private MyProperties2 myProperties2; @RequestMapping("/show2") public void show2() { Map<String, MyProperties2.UserInfoConfig> config = myProperties2.getConfig(); config.forEach((user, userInfoConfig) -> { System.out.println("user = " + user); System.out.println("userInfoConfig.getName() = " + userInfoConfig.getName()); System.out.println("userInfoConfig.getAge() = " + userInfoConfig.getAge()); System.out.println("userInfoConfig.getAddress() = " + userInfoConfig.getAddress()); }); }
打印結(jié)果:
到此這篇關(guān)于SpringBoot項(xiàng)目獲取統(tǒng)一前綴配置以及獲取非確定名稱配置的文章就介紹到這了,更多相關(guān)SpringBoot獲取統(tǒng)一前綴配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何利用Spring?Boot?監(jiān)控?SQL?運(yùn)行情況
這篇文章主要介紹了如何利用Spring?Boot監(jiān)控SQL運(yùn)行情況,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07java.lang.NullPointerException出現(xiàn)的幾種原因及解決方案
這篇文章主要介紹了java.lang.NullPointerException出現(xiàn)的幾種原因及解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05Java?Socket實(shí)現(xiàn)文件發(fā)送和接收功能以及遇到的Bug問題
這篇文章主要介紹了Java?Socket實(shí)現(xiàn)文件發(fā)送和接收功能以及遇到的Bug問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08SWT(JFace) FTP客戶端實(shí)現(xiàn)
SWT(JFace)小制作:FTP客戶端實(shí)現(xiàn)2009-06-06SpringBoot注解篇之@Resource與@Autowired的使用區(qū)別
@Resource 注解和 @Autowired 注解都是在 Spring Framework 中進(jìn)行依賴注入的注解,那么你知道他們有什么區(qū)別嗎,本文就來介紹一下2023-12-12關(guān)于TransmittableThreadLocal線程池中線程復(fù)用問題的解決方案
這篇文章主要介紹了關(guān)于TransmittableThreadLocal線程池中線程復(fù)用問題的解決方案,線程池復(fù)用線程,如果子線程執(zhí)行完未移除上下文,則會(huì)導(dǎo)致后續(xù)線程可以取到之前線程設(shè)置的屬性,需要的朋友可以參考下2023-11-11vscode搭建java開發(fā)環(huán)境的實(shí)現(xiàn)步驟
本文主要介紹了vscode搭建java開發(fā)環(huán)境,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Java使用Semaphore對(duì)單接口進(jìn)行限流
本篇主要講如何使用Semaphore對(duì)單接口進(jìn)行限流,主要有三種方式,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07