Loading

SpringBoot用@ConfigurationProperties获取各类配置文件

测试代码

MyConfigurationProperties

项目为SpringBoot项目,在Postman中调/showValue即可看到结果。本文中例子都是基于properties文件。

package com.example.configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;

@ConfigurationProperties(prefix = "com.demo")
@Component
public class MyConfigurationProperties {

    public String myString;
    public int myInt;
    public boolean myBoolean;
    public char myChar;

    public List<String> myList;
    public Set<String> mySet;
    public Map<String, String> myMap;

    public Student student;

    public List<Student> myStudentList;
    public Set<Student> myStudentSet;
    public Map<String, Student> myStudentMap;
    
    // getter setter 省略

}

IndexController:测试类

package com.example.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

    @Autowired
    public MyConfigurationProperties myConfigurationProperties;

    @RequestMapping("/showValue")
    public MyConfigurationProperties showValue() {
        return myConfigurationProperties;
    }
}

Student:一个简单实体类

package com.example.configuration;

public class Student {

    public String name;
    public int age;
    public Boolean boy;

    // getter setter 省略
    
}

测试结果

测试结果与下文对应

{
    "myString": "Hello World",
    "myInt": 2020,
    "myBoolean": true,
    "myChar": "t",
    "myList": [
        "list5",
        "list4",
        "list3",
        "list2",
        "list1",
        "list1"
    ],
    "mySet": [
        "set1",
        "set2",
        "set3",
        "set4",
        "set5"
    ],
    "myMap": {
        "k1": "v1",
        "k2": "v2",
        "k3": "v3"
    },
    "student": {
        "name": "Han Meimei",
        "age": 18,
        "boy": false
    },
    "myStudentList": [
        {
            "name": "LiLei",
            "age": 20,
            "boy": true
        },
        {
            "name": "Han Meimei",
            "age": 18,
            "boy": false
        }
    ],
    "myStudentSet": [
        {
            "name": "LiLei",
            "age": 20,
            "boy": true
        },
        {
            "name": "LiLei",
            "age": 20,
            "boy": true
        },
        {
            "name": "Han Meimei",
            "age": 20,
            "boy": false
        }
    ],
    "myStudentMap": {
        "boy": {
            "name": "LiLei",
            "age": 20,
            "boy": true
        },
        "girl": {
            "name": "Han Meimei",
            "age": 18,
            "boy": false
        }
    }
}

基本类型

com.demo.my-string=Hello World
com.demo.my-int=2020
com.demo.my-boolean=true
com.demo.my-char=t

String的List,Set,Map

com.demo.my-list=list5,list4,list3,list2,list1,list1
com.demo.my-set=set1,set2,set3,set4,set5,set5
com.demo.my-map.k1=v1
com.demo.my-map.k2=v2
com.demo.my-map.k3=v3

这里我看网上好多写法都是下面这样,个人观点除非value中有逗号否则真的没有必要

com.demo.my-list[0]=list0
com.demo.my-list[1]=list1
com.demo.my-set[0]=set0
com.demo.my-set[1]=set1

注意:Set中如果有重复的值,只会保存一次,上面两种用法都是

一般实体类

com.demo.student.name=Han Meimei
com.demo.student.age=18
com.demo.student.boy=false

实体类的List,Set,Map

com.demo.my-student-list[0].name=LiLei
com.demo.my-student-list[0].age=20
com.demo.my-student-list[0].boy=true
com.demo.my-student-list[1].name=Han Meimei
com.demo.my-student-list[1].age=18
com.demo.my-student-list[1].boy=false

com.demo.my-student-set[0].name=LiLei
com.demo.my-student-set[0].age=20
com.demo.my-student-set[0].boy=true
com.demo.my-student-set[1].name=LiLei
com.demo.my-student-set[1].age=20
com.demo.my-student-set[1].boy=true
com.demo.my-student-set[2].name=Han Meimei
com.demo.my-student-set[2].age=20
com.demo.my-student-set[2].boy=false

com.demo.my-student-map.boy.name=LiLei
com.demo.my-student-map.boy.age=20
com.demo.my-student-map.boy.boy=true
com.demo.my-student-map.girl.name=Han Meimei
com.demo.my-student-map.girl.age=18
com.demo.my-student-map.girl.boy=false

注意:这里面set[0]set[1]的实体类属性都是一样的, 但在上面的测试结果中,李雷还是返回了2次,这和String的Set是不一样的

总结

其实也没有什么好总结的,com.demo是前缀,那么前缀之后紧接的一级key就是Java类中的变量名,再在代码中看这个变量的类型或者看之前的人怎么用的,很快就能知道用法了。

附:完整的properties文件

com.demo.my-string=Hello World
com.demo.my-int=2020
com.demo.my-boolean=true
com.demo.my-char=t

com.demo.my-list=list5,list4,list3,list2,list1,list1
com.demo.my-set=set1,set2,set3,set4,set5,set5
com.demo.my-map.k1=v1
com.demo.my-map.k2=v2
com.demo.my-map.k3=v3

com.demo.student.name=Han Meimei
com.demo.student.age=18
com.demo.student.boy=false

com.demo.my-student-list[0].name=LiLei
com.demo.my-student-list[0].age=20
com.demo.my-student-list[0].boy=true
com.demo.my-student-list[1].name=Han Meimei
com.demo.my-student-list[1].age=18
com.demo.my-student-list[1].boy=false

com.demo.my-student-set[0].name=LiLei
com.demo.my-student-set[0].age=20
com.demo.my-student-set[0].boy=true
com.demo.my-student-set[1].name=LiLei
com.demo.my-student-set[1].age=20
com.demo.my-student-set[1].boy=true
com.demo.my-student-set[2].name=Han Meimei
com.demo.my-student-set[2].age=20
com.demo.my-student-set[2].boy=false

com.demo.my-student-map.boy.name=LiLei
com.demo.my-student-map.boy.age=20
com.demo.my-student-map.boy.boy=true
com.demo.my-student-map.girl.name=Han Meimei
com.demo.my-student-map.girl.age=18
com.demo.my-student-map.girl.boy=false

附:完整的yaml文件

com:
  demo:
    my-boolean: true
    my-char: t
    my-int: 2020
    my-list: list5,list4,list3,list2,list1,list1
    my-map:
      k1: v1
      k2: v2
      k3: v3
    my-set: set1,set2,set3,set4,set5,set5
    my-string: Hello World
    my-student-list:
    - age: 20
      boy: true
      name: LiLei
    - age: 18
      boy: false
      name: Han Meimei
    my-student-map:
      boy:
        age: 20
        boy: true
        name: LiLei
      girl:
        age: 18
        boy: false
        name: Han Meimei
    my-student-set:
    - age: 20
      boy: true
      name: LiLei
    - age: 20
      boy: true
      name: LiLei
    - age: 20
      boy: false
      name: Han Meimei
    student:
      age: 18
      boy: false
      name: Han Meimei

还是觉得properties文件比较好懂

posted @ 2020-07-01 22:27  n031  阅读(806)  评论(0编辑  收藏  举报