开发者

SpringBoot项目获取统一前缀配置及获取非确定名称配置方法

开发者 https://www.devze.com 2024-09-10 10:17 出处:网络 作者: gooluke
目录SpringBoot项目获取统一前缀配置以及获取非确定名称配置方式一:使用对应的配置类并结合注解:MjQwCeRyv@ConfigurationProperties(prefix = “xxx.xxx”)方式二:获取统一前缀,而后面非确定字段名的配
目录
  • SpringBoot项目获取统一前缀配置以及获取非确定名称配置
    • 方式一:使用对应的配置类并结合注解:MjQwCeRyv@ConfigurationProperties(prefix = “xxx.xxx”)
    • 方式二:获取统一前缀,而后面非确定字段名的配置

SpringBoot项目获取统一前缀配置以及获取非确定名称配置

在SpringBoot项目中,我们经常看到统一前缀的配置,我们该怎么统一获取

my.config.a.name=xiaoming

my.config.a.age=18

my.config.a.address=guangdonpythong

my.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.getAddre编程客栈ss());
    }
}

打印结果:

SpringBoot项目获取统一前缀配置及获取非确定名称配置方法

方式二:获取统一前缀,而后面非确定字段名的配置

配置文件:

my.config.a.name=xiaoming
my.confjavascriptig.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() = " + userIn编程foConfig.getAddress());
    });
}

打印结果:

SpringBoot项目获取统一前缀配置及获取非确定名称配置方法

到此这篇关于SpringBoot项目获取统一前缀配置以及获取非确定名称配置的文章就介绍到这了,更多相关SpringBoot获取统一前缀配置内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号