spring注入yml和properties中配置

基本类型+字符串

配置文件yml

configInt: 1
configShort: 1
configLong: 1
configDouble: 1.0
configFloat: 1.0
configBoolean: true
configChar: 1
configByte: 1
configString: "configString"

java

@Component
public class TestConfig {
    @Value("${configInt}")
    int configInt;
    @Value("${configShort}")
    short configShort;
    @Value("${configLong}")
    long configLong;
    @Value("${configDouble}")
    double configDouble;
    @Value("${configFloat}")
    float configFloat;
    @Value("${configBoolean}")
    boolean configBoolean;
    @Value("${configChar}")
    char configChar;
    @Value("${configByte}")
    byte configByte;
    @Value("${configString}")
    String configString;

    @PostConstruct
    void test(){
        System.out.println("configInt: " + configInt);
        System.out.println("configShort: " + configShort);
        System.out.println("configLong: " + configLong);
        System.out.println("configDouble: " + configDouble);
        System.out.println("configFloat: " + configFloat);
        System.out.println("configBoolean: " + configBoolean);
        System.out.println("configChar: " + configChar);
        System.out.println("configByte: " + configByte);
        System.out.println("configString: " + configString);
    }
}

结果

set和list一样

这边以set为例

set<String>

方式一

set1: 111,222,333
@Component
public class TestConfig {
    @Value("${set1}")
    private Set<String> set1;
    @PostConstruct
    void test(){
        System.out.println(set1);
    }
}

 

方式二

set:
  set2:
    - 1111
    - 2222
    - 3333
@Component
@ConfigurationProperties(prefix = "set")
public class TestConfig {
    @Setter
    private Set<String> set2;
    @PostConstruct
    void test(){
        System.out.println(set2);
    }
}

set<List>

set:
  set3:
    - 1,2,3,4,5
    - 3,4,5,6
    - 5,6,7
@Component
@ConfigurationProperties(prefix = "set")
public class TestConfig {
    @Setter
    private Set<List<String>> set3;

    @PostConstruct
    void test(){
        System.out.println(set3);
    }
}

 set<Map>

set:
  set4:
    - key1: value1
      key2: value2
      key3: value3
    - key4: value4
      key5: value5
      key6: value6
@Component
@ConfigurationProperties(prefix = "set")
public class TestConfig {
    @Setter
    private Set<Map<String,String>> set4;
    @PostConstruct
    void test(){

        System.out.println(set4);
    }
}

set<Stu> 

Stu是自定义对象

@Data
public class Stu {
    String name;
    Integer age;
}
set:
  set5:
    - name: 张三
      age: 11
    - name: 李四
      age: 25
@Component
@ConfigurationProperties(prefix = "set")
public class TestConfig {
    @Setter
    private Set<Stu> set5;

    @PostConstruct
    void test(){

        System.out.println(set5);
    }
}

map

Map<String,String>

方式一

map:
  map1:
    - key: zhangsan
      value: 11
    - key: lisi
      value: 22
@Component
@ConfigurationProperties(prefix = "map")
public class TestConfig {
    @Setter
    private Map<String,String> map1;
    @PostConstruct
    void test(){
        System.out.println(map1);
    }
}

这样写key和value前面会有索引id

方式二 

map:
  map1:
    zhangsan: 11
    lisi: 22
@Component
@ConfigurationProperties(prefix = "map")
public class TestConfig {
    @Setter
    private Map<String,String> map1;
    @PostConstruct
    void test(){
        System.out.println(map1);
    }
}

Map<String,List<String>>

方式一

map:
  map1:
    zhangsan: 11,22,33
    lisi: 111,222,333
@Component
@ConfigurationProperties(prefix = "map")
public class TestConfig {
    @Setter
    private Map<String,List<String>> map1;
    @PostConstruct
    void test(){
        System.out.println(map1);
    }
}

方式二 

map:
  map1:
    zhangsan:
      - 11
      - 22
      - 33
    lisi:
      - 111
      - 222
      - 333
@Component
@ConfigurationProperties(prefix = "map")
public class TestConfig {
    @Setter
    private Map<String,List<String>> map1;
    @PostConstruct
    void test(){
        System.out.println(map1);
    }
}

Map<String,Stu>

Stu是自定义对象

@Data
public class Stu {
    String name;
    Integer age;
}
map:
  map1:
    zhangsan:
      name: 张三
      age: 11
    lisi:
      name: 李四
      age: 22
@Component
@ConfigurationProperties(prefix = "map")
public class TestConfig {
    @Setter
    private Map<String,Stu> map1;
    @PostConstruct
    void test(){
        System.out.println(map1);
    }
}

对象中值注入

object:
  name: 张三
  gender: 男
  stu:
    name: stu
    age: 11
  list:
    - name: list张三
      age: 11
    - name: list李四
      age: 22
  map:
    zhangsan:
      name: map张三
      age: 11
    lisi:
      name: map李四
      age: 22
@Component
@ConfigurationProperties(prefix = "object")
public class TestConfig {
    @Setter
    String name;
    @Setter
    String gender;
    @Setter
    Stu stu;
    @Setter
    List<Stu> list;

    @Setter
    private Map<String,Stu> map;
    @PostConstruct
    void test(){
        System.out.println("name:"+name);
        System.out.println("gender"+gender);
        System.out.println("stu"+stu);
        System.out.println("list"+list);
        System.out.println("map"+map);
    }
}

总结 

遇到简单类型都可以直接使用@Value("${name}")取值

遇到复杂的,如list,set,map,自定义对象都建议使用

@ConfigurationProperties(prefix = "object")加setter方法

注意使用@ConfigurationProperties(prefix = "object")时,prefix不能有大写字母,否则会报错,建议使用-替换大写为小写,如userInfo=>>>user-info

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值