likes
comments
collection
share

【SpringBoot】配置文件的加载与属性值的绑定

作者站长头像
站长
· 阅读数 15

Question


在使用SpringBoot过程中你是否会有以下疑问?

  • 具体有多少种配置属性源的方式呢?
  • 为何使用@Value 注解就能够获取到属性源中的值呢?
  • 属性源这么多,如果属性相同的话 那么用哪个值呢?
  • 属性源是如何绑定到我们的程序中的呢?

本篇文章会针对以上问题逐个分析

Answer


我们的所有属性源都存放在AbstractEnvironment中的属性propertySources中; 每加载一个属性源就会往里面塞一个propertySource; 然后当我们需要取某个属性的时候,就会从这个propertySources遍历查找,找到就返回; 所以我们就可以知道,如果多个属性源中有相同的属性,那么肯定是排在最前面的被找到就会返回,优先级最高; 那么这是整个背景; 我们现在来分析具体的问题

具体有多少种配置属性源的方式呢?

以下优先级由高到低

  1. 命令行方式 java -jar xx.jar --spring.profiles.active=pro& 关于命令行的详细请看文章 【SpringBoot 一】SpringApplication启动类的Args详解

  2. 如果是以web方式启动的还会有 {servletConfigInitParamsservletContextInitParams}

  3. spring.application.json 外部json配置 在启动之初,SpringBoot会去当前的属性源(这个时候还只有systemPropertiessystemEnvironment)中查找有没有spring.application.json或者SPRING_APPLICATION_JSON的属性值;如果有则会把对应的值按照Json的格式解析成对应的属性源;例如: java -jar xx.jar --spring.application.json='{"foo":"bar"}' java -jar xx.jar -Dspring.application.json={\"foo\":\"888\"} 如果这2种方式都用,那么以第一种命令行的方式为准,它的优先级更高

  4. systemProperties JVM属性源; 使用方式就是 java -jar xx.jar -Dmyname=src

  5. systemEnvironment系统环境变量属性源

  6. random随机数属性源 RandomValuePropertySource 我们可以通过获取属性key = random.int 来获取随机值

  7. 配置文件属性源 application.properties这样的配置文件

  8. 注解@PropertySources的属性源

  9. 通过SpringApplication.setDefaultProperties声明的默认属性源;

|方式 |用法 |描述| |--|--|--|--| | 命令行方式(启动的Args参数) | java -jar xx.jar --spring.profiles.active=pro |args用法详解| |spring.application.json 外部json配置|java -jar xx.jar --spring.application.json='{"foo":"bar"}' java -jar xx.jar -Dspring.application.json={"foo":"888"}|在启动之初,SpringBoot会去当前的属性源(这个时候还只有systemProperties、systemEnvironment)中查找有没有spring.application.json或者SPRING_APPLICATION_JSON的属性值;如果有则会把对应的值按照Json的格式解析成对应的属性源| |JVM属性源|java -jar xx.jar -Dmyname=src| | |系统环境变量属性源|自动读取环境变量属性| |随机数属性源 RandomValuePropertySource|random.int 、random.long、random.int.5,100; 、|在SpringBoot中使用以上key可以获得指定的随机值| |配置文件application.properties||| |注解@PropertySources的属性源|可以把属性配置在另外单独的文件中,使用注解也可以加载为属性源|| |SpringApplication.setDefaultProperties声明的默认属性源|||

属性源这么多,如果属性相同的话 那么用哪个值呢?

属性源是一个List,读取的时候是遍历List; 先读取到的立马返回; 优先级的顺序是上面1-9种方式;

为何使用@Value 注解就能够获取到属性源中的值呢?

我们先介绍一下@Value的几种常用用法

        //常量
    @Value("#{1}")
    private int constant;

    //从属性源取值
    @Value("${test.name}")
    private String name;

    //从属性源取值
    @Value("${test.name2: defaultname}")
    private String namedefault;

    //从容器中获取bean的的属性值
    @Value("#{developerProperty.name}")
    private String dname;

    //从指定属性源获取属性值(jvm属性)
    @Value("#{systemProperties['spring.application.json']}")
    private String systemPropertiesjson;

    //从指定属性源获取属性值(系统环境属性源)
    @Value("#{systemEnvironment['HOME']}")
    private String systemEnvironmentHOME;

    //从指定属性源获取属性值 默认值
    @Value("#{systemEnvironment['HOME22']?:'default'}")
    private String systemEnvironmentHOMEdefault;

    //获取随机值
    @Value("${random.int.5,100;}")
    private Integer randomint;

SpringBoot 中 @Value 源码解析

属性源是如何绑定到我们的程序中的呢?

先看看用法; 下面是SpringBoot启动过程中 将配置spring.main开头的属性 绑定到 SpringApplication中的用法

protected void bindToSpringApplication(ConfigurableEnvironment environment) {
		try {
			Binder.get(environment).bind("spring.main", Bindable.ofInstance(this));
		}
		catch (Exception ex) {
			throw new IllegalStateException("Cannot bind to SpringApplication", ex);
		}
	}

绑定到实例中

那我们自己来写一个demo将配置文件的属性值绑定到某个类实例中;

public class BinderTest {


    private String bname;

    private Integer bage;

    private BinderInnerTest binderInnerTest;


    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public Integer getBage() {
        return bage;
    }

    public void setBage(Integer bage) {
        this.bage = bage;
    }

    public BinderInnerTest getBinderInnerTest() {
        return binderInnerTest;
    }

    public void setBinderInnerTest(BinderInnerTest binderInnerTest) {
        this.binderInnerTest = binderInnerTest;
    }

    public static class BinderInnerTest{

        private String innerName;

        private Integer innerage;

        public String getInnerName() {
            return innerName;
        }

        public void setInnerName(String innerName) {
            this.innerName = innerName;
        }

        public Integer getInnerage() {
            return innerage;
        }

        public void setInnerage(Integer innerage) {
            this.innerage = innerage;
        }
    }


}


PS:上面要注意. set get 不能少, 而且如果是内部类,必须要是 public static class 否则内部类的属性不会正确绑定的!

配置文件

binder.test.bname=bindname
binder.test.bage=18
binder.test.binderInnerTest.innerName=bindInnername
binder.test.binderInnerTest.innerage=28

绑定


BindResult<BinderTest> result = Binder.get(environment).bind("binder.test", Bindable.of(BinderTest.class));
System.out.println(result);

绑定成功 【SpringBoot】配置文件的加载与属性值的绑定 为何 binder.test 这种前缀就能把实例属性给绑定上呢? Binder属性绑定源码解析 TODO。。。。

有没有觉得这种方式很熟悉?SpringBoot 中有个注解@ConfigurationProperties(prefix = "") 的功能是不差不多?也是将属性值绑定到实例中去; 那么它是怎么实现的呢? 是不是也是通过Binder的方式实现的? 答案是肯定的,贴一个关键代码 ConfigurationPropertiesBinder

	public void bind(Bindable<?> target) {
		ConfigurationProperties annotation = target
				.getAnnotation(ConfigurationProperties.class);
		Assert.state(annotation != null,
				() -> "Missing @ConfigurationProperties on " + target);
		List<Validator> validators = getValidators(target);
		BindHandler bindHandler = getBindHandler(annotation, validators);
		getBinder().bind(annotation.prefix(), target, bindHandler);
	}

详细分析 ConfigurationProperties TODO..

绑定到List中

配置文件

binder.test2.list[0]=valuea
binder.test2.list[1]=valueb
binder.test2.list[2]=valuec
binder.test2.list[3]=valued

PS: 数组index 必须连续

绑定

        
    BindResult<List<String>> resultlist = Binder.get(environment).bind("binder.test2.list", Bindable.listOf(String.class));

绑定成功 【SpringBoot】配置文件的加载与属性值的绑定

绑定到Map中

配置文件

binder.test3.a=a
binder.test3.b=b
binder.test3.c=c

绑定


    BindResult<Map<String, String>> resultmap = Binder.get(environment).bind("binder.test3", Bindable.mapOf(String.class,String.class));

绑定成功 【SpringBoot】配置文件的加载与属性值的绑定

PS: 如果多个属性源中有相同的属性源前缀会如何?那么会按照属性源的优先级绑定;后面的不再绑定

本文正在参加「金石计划」