• SPRINGBOOT04_配置文件、三种读取配置文件方式


    ①. yaml配置文件

    • ①. YAML全称是 YAML Aint Markup Language。YAML是一种直观能够被电脑识别的数据数据序列化格式,并且容易被人阅读,容易和脚本语言交互的,可以被支持YAML库的不同的编程语言程序导入,比如:C/C++ ,JAVA ,PHP等。YAML文件扩展名可以使用.yml 或者 .yaml

    • ②. YAML的基本语法

    1. 大小写敏感,key: value;kv之间有空格
    2. 数据值前边必须有空格,作为分隔符
    3. 使用缩进表示层级关系(缩进的空格数目不重要,只要保证同层级的元素左侧对齐即可)
    4. 缩进时不允许使用Tab键,只允许使用空格(各个系统Tab对应的空格数目可能不同,导致层级混乱)
    5. #表示注释,从这个字符一直到行尾,都会被解析器忽略
    • ③. 数据类型
    # (1). 字面量:单个的、不可再分的值。date、booleanstringnumbernull
    k: v
    # (2). 对象:键值对的集合。map、hash、set、object
    行内写法:  k: {k1:v1,k2:v2,k3:v3}
    #或
    k: 
      k1: v1
      k2: v2
      k3: v3
    # (3). 数组:一组按次序排列的值。array、list、queue
    行内写法: k: [v1,v2,v3]
    #或者
    k:
     - v1
     - v2
     - v3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • ④. yaml代码展示
    server:
      port: 8888
    mycar:
      branch: "雅阁"
      price: "20w"
    name: abc
    #1.对象(map)键值对的集合
    person:
      name: TANGZHI #${name}
      age: 24
      birthday: 2021/05/24 20:12:33
      address:
        - beijing
        - shanghai
      # List集合
      animal:
        - 猴子
          大象
        - 青蛙
      # Map集合
      score:
        english:
          first: 30
          second: 40
          third: 50
        math:
          100
      # Set集合
      salary:
        - 1000
        - 2000
    #行内写法
    address2: [beijing2,shanghai2]
    #3.纯量
    msg1: 'hello \n word' # 这里正常输出
    msg2: "hello \n word" # 这里会带上转行符号
    #spring:
    #  banner:
    #    image:
    #      location: classpath:bug.png
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    ②. 读取配置的三种方式

    • ①. 使用@Value(“${server.port}”)

    • ②. 注入Environment

    • ③. ConfigurationProperties(prefix=’ ')

    • ④. 代码展示

    server:
      port: 8888
    mycar:
      branch: "雅阁"
      price: "20w"
    name: abc
    #1.对象(map)键值对的集合
    person:
      name: TANGZHI #${name}
      age: 24
      birthday: 2021/05/24 20:12:33
      address:
        - beijing
        - shanghai
      # List集合
      animal:
        - 猴子
          大象
        - 青蛙
      # Map集合
      score:
        english:
          first: 30
          second: 40
          third: 50
        math:
          100
      # Set集合
      salary:
        - 1000
        - 2000
    #行内写法
    address2: [beijing2,shanghai2]
    #3.纯量
    msg1: 'hello \n word' # 这里正常输出
    msg2: "hello \n word" # 这里会带上转行符号
    #spring:
    #  banner:
    #    image:
    #      location: classpath:bug.png
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    // 第一种方式,使用Component+ConfigurationProperties注解的方式加载yaml文件
    @Component
    @ConfigurationProperties(prefix = "person")
    public class Person {
        private String name;
        private Integer age;
        private Date birthday;
        private String[]address;
        private List<String> animal;
        private Map<String,Object> score;
        private Set<Double> salary;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    @Slf4j
    @RestController
    public class myController {
        /*(1).通过@value注解从配置文件中获取值*/
        @Value("${msg1}")
        private String msg1;
        @Value("${msg2}")
        private String msg2;
        /*2.Environment*/
        @Autowired
        private Environment env;
        @Autowired
        Car car;
        //(3). 通过ConfigurationProperties注解进行绑定
        @Autowired
        Person person;
    
        @GetMapping("/car")
        public Car getMyCar(){
            log.info("***********");
            log.info("***********");
            return car;
        }
        @GetMapping("/person")
        public Person getPerson(){
            //msg1:hello \n word
            System.out.println("msg1:"+msg1);
            /**
             * msg2:hello
             // word
             */
            System.out.println("msg2:"+msg2);
            String address = env.getProperty("address2[0]");
            //beijing2
            System.out.println(address);
            return person;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    在这里插入图片描述

  • 相关阅读:
    未整理的知识链接
    性能测试-性能工程落地的4个阶段(21)
    距离度量 —— 汉明距离(Hamming Distance)
    论 shared_ptr的线程安全
    正则表达式验证和跨域postmessage
    老司机带你快速实现Python下载与安装
    C++基础入门 --- 【学习指南】
    k8s--基础--29.2--ingress--安装Ingress Controller和配置Ingress
    第二篇 渲染框架2.x
    C语言 每日一题 PTA 10.21-10.24日 day3
  • 原文地址:https://blog.csdn.net/TZ845195485/article/details/126575046