• SpringBoot SpringBoot 基础篇(第一篇) 第2章 SpringBoot 全局配置 2.3 yaml 读取数据


    SpringBoot

    【千锋教育java教程SpringBoot2全套,springboot快速入门到项目实战视频教程】

    SpringBoot 基础篇(第一篇)

    第2章 SpringBoot 全局配置

    2.3 yaml 读取数据
    2.3.1 读取配置文件数据

    yaml中保存的单个数据,可以使用Spring中的注解直接读取,

    使用@value可以读取单个数据,属性名引用方式∶${一级属性名.二级属性名…}

    server:
      port: 81
    
    #字面值
    username: zhangsan
    city: beijing
    birthday: 2022-10-14
    money: 10.9
    port: 8080
    
    # 对象
    person:
      name: qfedu
      age: 10
    
    # 数组类型
    hobby:
      - zuqiu
      - lanqiu
      - paiqiu
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    package com.dingjiaxiong.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Arrays;
    
    /**
     * ClassName: HelloController
     * date: 2022/10/14 15:03
     *
     * @author DingJiaxiong
     */
    
    @RestController
    public class HelloController {
    
        //读取配置文件中的数据
        //字符串数据
        @Value("${username}")
        private String username;
    
        @Value("${city}")
        private String city;
    
        @Value("${server.port}")
        private int port;
    
        @Value("${person.name}")
        private String name;
    
        @Value("${hobby[1]}")
        private String[] hobby;
    
        @RequestMapping("/hello")
        public String hello(){
    
            System.out.println("username = " + username);
            System.out.println("city = " + city);
            System.out.println("port = " + port);
            System.out.println("name = " + name);
            System.out.println("hobby = " + Arrays.toString(hobby));
    
            return "Hello world!!!";
        }
    
    }
    
    • 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
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    运行结果

    在这里插入图片描述

    用户名【操作系统的】

    在这里插入图片描述

    这里u1s1,没黑马李老师讲得好

    2.3.2 读取全部配置文件数据

    读取单—数据可以解决读取数据的问题,但是如果定义的数据量过大,这么一个一个书写肯定会累死人的,

    SpringBoot提供了一个对象,能够把所有的数据都封装到这一个对象中,这个对象叫做Environment,使用自动装配注解可以将所有的yaml数据封装到这个对象中

    package com.dingjiaxiong.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.core.env.Environment;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Arrays;
    
    /**
     * ClassName: HelloController
     * date: 2022/10/14 15:03
     *
     * @author DingJiaxiong
     */
    
    @RestController
    public class HelloController {
    
        @Autowired
        private Environment environment;
    
    
        @RequestMapping("/hello")
        public String hello(){
    
            System.out.println(environment.getProperty("city"));
            System.out.println(environment.getProperty("server.port"));
            System.out.println(environment.getProperty("person.name"));
            System.out.println(environment.getProperty("hobby[2]"));
            System.out.println(environment.getProperty("environment"));
    
            return "Hello world!!!";
        }
    
    }
    
    • 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

    运行结果:

    在这里插入图片描述

    数据封装到了Environment对象中,获取属性时,通过Environment的接口操作进行,具体方法时getProperties (String),参数填写属性名即可

    【总结】

    • 使用Environment对象封装全部配置信息
    • 使用@Autowired自动装配数据到Environment对象中
    2.3.3 读取对象数据

    Java是一个面向对象的语言,很多情况下,我们会将一组数据封装成一个对象。

    SpringBoot也提供了可以将一组yaml对象数据封装一个Java对象的操作

    首先定义一个对象,并将该对象纳入Spring管控的范围,也就是定义成一个bean,然后使用注解@ConfigurationProperties指定该对象加载哪一组yaml中配置的信息。

    这个@ConfigurationProperties必须告诉他加载的数据前缀是什么,这样当前前缀下的所有属性就封装到这个对象中。记得数据属性名要与对象的变量名一一对应,不然没法封装。

    其实以后如果你要定义一组数据自己使用,就可以先写一个对象,然后定义好属性,下面到配置中根据这个格式书写即可。

    举个栗子:

    datasource:
      driver: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/db?serverTimezone=UTC
      username: root
      password: 200039
    
    • 1
    • 2
    • 3
    • 4
    • 5

    这是配置文件中的一个“对象”。

    新建一个类

    package com.dingjiaxiong.data;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    /**
     * ClassName: UserDataSource
     * date: 2022/10/14 17:00
     *
     * @author DingJiaxiong
     */
    
    @Component
    @ConfigurationProperties(prefix = "datasource")
    public class UserDataSource {
    
        private String driver;
        private String url;
        private String username;
        private String password;
    
        public UserDataSource() {
        }
    
        public UserDataSource(String driver, String url, String username, String password) {
            this.driver = driver;
            this.url = url;
            this.username = username;
            this.password = password;
        }
    
    
        public String getDriver() {
            return driver;
        }
    
        public void setDriver(String driver) {
            this.driver = driver;
        }
    
        public String getUrl() {
            return url;
        }
    
        public void setUrl(String url) {
            this.url = url;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        @Override
        public String toString() {
            return "UserDataSource{" +
                    "driver='" + driver + '\'' +
                    ", url='" + url + '\'' +
                    ", username='" + username + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }
    
    • 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
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    同样在控制器中自动注入

    package com.dingjiaxiong.controller;
    
    import com.dingjiaxiong.data.UserDataSource;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.core.env.Environment;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Arrays;
    
    /**
     * ClassName: HelloController
     * date: 2022/10/14 15:03
     *
     * @author DingJiaxiong
     */
    
    @RestController
    public class HelloController {
    
        @Autowired
        private UserDataSource userDataSource;
    
        @RequestMapping("/hello")
        public String hello(){
    
            System.out.println(userDataSource.getDriver());
            System.out.println(userDataSource.getUrl());
            System.out.println(userDataSource.getUsername());
            System.out.println(userDataSource.getPassword());
    
            return "Hello world!!!";
        }
    
    }
    
    • 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

    访问接口测试

    在这里插入图片描述

    OK。没毛病。

    当然咱们之前那个person 对象也就同样的道理了

    package com.dingjiaxiong.data;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    /**
     * ClassName: PersonData
     * date: 2022/10/14 17:12
     *
     * @author DingJiaxiong
     */
    
    @Component
    @ConfigurationProperties(prefix = "person")
    public class PersonData {
    
        private String name;
        private int age;
    
        public PersonData() {
        }
    
        public PersonData(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "PersonData{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    • 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
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    测试结果

    在这里插入图片描述

    OK。

  • 相关阅读:
    Windows Server 2022 简体中文版、英文版下载 (updated Jun 2022)
    用于数据分析和数据科学的SQL教程
    Linux epoll 编程些许浅谈
    vue组件传参
    【Java设计模式 SOLID设计原则】四 ISP接口隔离原则
    【教学类-14-01】20221113《图形数量统计6*6-2》(大班主题《》)
    【路径规划】基于FMM快速行进法实现船舶路径规划附matlab代码
    浅谈数据结构之队列
    python之时间、时间戳、时间加减
    Java后端获取当前项目所在路径的方法(适用于Linux、Windows)
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/127696675