SpringBoot項目獲取統(tǒng)一前綴配置及獲取非確定名稱配置方法
SpringBoot項目獲取統(tǒng)一前綴配置以及獲取非確定名稱配置
在SpringBoot項目中,我們經??吹浇y(tǒng)一前綴的配置,我們該怎么統(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
方式一:使用對應的配置類并結合注解:@ConfigurationProperties(prefix = “xxx.xxx”)
配置文件:
my.config.name=xiaoming my.config.age=18 my.config.address=guangdong
對應的配置類: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());
}
}打印結果:

方式二:獲取統(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
對應的配置類:
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得對應上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());
});
}打印結果:

到此這篇關于SpringBoot項目獲取統(tǒng)一前綴配置以及獲取非確定名稱配置的文章就介紹到這了,更多相關SpringBoot獲取統(tǒng)一前綴配置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于SpringBoot+JWT 實現Token登錄認證與登錄人信息查詢功能
本文給大家介紹基于SpringBoot+JWT實現Token登錄認證與登錄人信息查詢功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值2026-03-03
SpringBoot中的@ConditionalOnMissingBean注解使用詳解
這篇文章主要介紹了SpringBoot中的@ConditionalOnMissingBean注解使用詳解,@ConditionalOnMissingBean作用在@Bean定義上,也就是說在容器加載它作用的Bean時,檢查容器中是否存在目標類型,需要的朋友可以參考下2024-01-01
Java網絡通信中ServerSocket的設計優(yōu)化方案
今天小編就為大家分享一篇關于Java網絡通信中ServerSocket的設計優(yōu)化方案,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04
Java使用阻塞隊列BlockingQueue實現生產者消費者的方法
BlockingQueue是一個支持阻塞插入和移除操作的隊列,常用于多線程環(huán)境下的生產者和消費者場景,文章介紹了阻塞隊列BlockingQueue的概念和其在生產者消費者模式中的應用,提供了一個簡單的示例,展示了如何使用ArrayBlockingQueue來實現生產者消費者模式2024-11-11

