美文网首页
ConfigurationProperties

ConfigurationProperties

作者: timar | 来源:发表于2019-08-09 17:49 被阅读0次

list注入

config-test1:
  name: jack
  age: 18

config-test2:
  name:
    - jack
    - rose
    - peter

@Configuration
@ConfigurationProperties(value = "config-test1")
public class Config1 {

    private String name;
    private int age;

  // 省略getter/setter
}

@Configuration
@ConfigurationProperties(value = "config-test2")
public class Config2 {

    private List<Object> name;

  // 省略getter/setter
}

最后设置值是在
org.springframework.boot.context.properties.bind.Binder#bindObject
org.springframework.boot.context.properties.bind.Binder#findProperty

    private ConfigurationProperty findProperty(ConfigurationPropertyName name,
            Context context) {
        if (name.isEmpty()) {
            return null;
        }
        // 这里SpringIterableConfigurationPropertySource("classpath:/application.yml")会取到值
        ## org.springframework.boot.context.properties.source.SpringIterableConfigurationPropertySource#getCache
        for (ConfigurationPropertySource source : context.getSources()) {
            ConfigurationProperty property = source.getConfigurationProperty(name);
            if (property != null) {
                return property;
            }
        }
        return null;
    }

读取yml文件为

image.png

原文地址:https://www.jianshu.com/p/c6ca1edcf6f5

# 读取配置文件测试数据
test.prop:
  # 字符串
  str: I am string.
  # 数组、列表
  strArray: string1,string2
  strList: string3,string4
  # map
  strMap: 
    key1: value1
    key2: value2
  authors:
    - name: corgiking
      sex: 男
      age: 25
    - name: goaler
      sex: 男
      age: 25
  innerProp:
    str: I am string.
    strArray: string5,string6
    strList: string7,string8
    strMap: 
      key3: value3
      key4: value4
原文地址:[https://www.jianshu.com/p/6a4510cfed2a](https://www.jianshu.com/p/6a4510cfed2a)

相关文章

网友评论

      本文标题:ConfigurationProperties

      本文链接:https://www.haomeiwen.com/subject/snzhjctx.html