• 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文件比较好懂

  • 相关阅读:
    STM32 GPIO 配置之ODR, BSRR, BRR 详解
    Understanding the STM32F0's GPIO
    STM32F4 External interrupts
    Calculate CAN bit timing parameters -- STM32
    禁用SQL Server Management Studio的IntelliSense
    SQL Server 2016中In-Memory OLTP继CTP3之后的新改进
    一张图解释SQL Server集群、镜像、复制、日志传送
    SQL Server出现错误: 4014
    SQL Server 2016五大优势挖掘企业用户数据价值
    SQL Server 2008, 2008 R2, 2012 and 2014 完全支持TLS1.2加密传输
  • 原文地址:https://www.cnblogs.com/n031/p/13221972.html
Copyright © 2020-2023  润新知