1. 配置文件格式

我们现在启动服务器默认的端口号是 8080,访问路径可以书写为

http://localhost:8080/books/1

在线上环境我们还是希望将端口号改为 80,这样在访问的时候就可以不写端口号了,如下

http://localhost/books/1

而 SpringBoot 程序如何修改呢?SpringBoot 提供了多种属性配置方式

注意:SpringBoot 程序的配置文件名必须是 application ,只是后缀名不同而已。

2. 不同配置文件演示

2.1 application.properties配置文件

启动服务,会在控制台打印出日志信息,从日志信息中可以看到绑定的端口号已经修改了

Tomcat initialized with port(s): 80 (http)

2.2 application.yml配置文件

删除 application.properties 配置文件中的内容。在 resources 下创建一个名为 application.yml 的配置文件,在该文件中书写端口号的配置项,格式如下:

server:
  port: 81

注意: 在:后,数据前一定要加空格。

启动服务,可以在控制台看到绑定的端口号是 81

Tomcat initialized with port(s): 81 (http)

2.3 application.yaml配置文件

删除 application.yml 配置文件和 application.properties 配置文件内容,然后在 resources 下创建名为 application.yaml 的配置文件,配置内容和后缀名为 yml 的配置文件中的内容相同,只是使用了不同的后缀名而已

application.yaml 配置文件内容如下:

server:
  port: 82

启动服务,在控制台可以看到绑定的端口号

Tomcat initialized with port(s): 82 (http)

3. 三种配合文件的优先级

在三种配合文件中分别配置不同的端口号,启动服务查看绑定的端口号。用这种方式就可以看到哪个配置文件的优先级更高一些

<aside> 🌷 注意:

</aside>

4. yaml格式

上面讲了三种不同类型的配置文件,而 properties 类型的配合文件之前我们学习过,接下来我们重点学习 yaml 类型的配置文件。

YAML(YAML Ain’t Markup Language),一种数据序列化格式。这种格式的配置文件在近些年已经占有主导地位,那么这种配置文件和前期使用的配置文件是有一些优势的,我们先看之前使用的配置文件。

通过对比,我们得出yaml的优点有:

YAML 文件扩展名:

上面两种后缀名都可以,以后使用更多的还是 yml 的。

4.1 yml语法规则

数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔,例如

enterprise:
  name: Helsing
  age: 16
  tel: 400-957-241
  subject:
    - Java
    - Python
    - C#

4.2 yaml配置文件数据读取

4.2.1 环境准备

修改resource目录下的application.yml配置文件

lesson: SpringBoot

server:
  port: 80

enterprise:
  name: Helsing
  age: 16
  tel: 400-957-241
  subject:
    - Java
    - Python
    - C#

com.blog.domain包下新建一个Enterprise类,用来封装数据

package com.blog.domain;

import java.util.Arrays;

public class Enterprise {
    private String name;
    private int age;
    private String tel;
    private String[] subject;

    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;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String[] getSubject() {
        return subject;
    }

    public void setSubject(String[] subject) {
        this.subject = subject;
    }

    @Override
    public String toString() {
        return "Enterprise{" +
                "name='" + name + '\\'' +
                ", age=" + age +
                ", tel='" + tel + '\\'' +
                ", subject=" + Arrays.toString(subject) +
                '}';
    }
}

4.2.2 读取配置文件

方式一:使用 @Value注解

@RestController
@RequestMapping("/books")
public class BookController {
    @Value("${lesson}")
    private String lesson;
    @Value("${server.port}")
    private Integer port;
    @Value("${enterprise.subject[0]}")
    private String subject_0;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id) {
        System.out.println(lesson);
        System.out.println(port);
        System.out.println(subject_0);
        return "hello , spring boot!";
    }
}

方式二:使用Environment对象

上面方式读取到的数据特别零散,SpringBoot 还可以使用 @Autowired 注解注入 Environment 对象的方式读取数据。这种方式 SpringBoot 会将配置文件中所有的数据封装到 Environment 对象中,如果需要使用哪个数据只需要通过调用 Environment 对象的 getProperty(String name) 方法获取。具体代码如下

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private Environment environment;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id) {
        System.out.println(environment.getProperty("lesson"));
        System.out.println(environment.getProperty("enterprise.name"));
        System.out.println(environment.getProperty("enterprise.subject[1]"));
        return "hello , spring boot!";
    }
}

方式三:使用自定义对象

@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
    private String name;
    private int age;
    private String tel;
    private String[] subject;

    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;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String[] getSubject() {
        return subject;
    }

    public void setSubject(String[] subject) {
        this.subject = subject;
    }

    @Override
    public String toString() {
        return "Enterprise{" +
                "name='" + name + '\\'' +
                ", age=" + age +
                ", tel='" + tel + '\\'' +
                ", subject=" + Arrays.toString(subject) +
                '}';
    }
}

BooKController内容如下

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private Enterprise enterprise;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id) {
        System.out.println(enterprise);
        System.out.println(enterprise.getAge());
        System.out.println(enterprise.getName());
        System.out.println(enterprise.getTel());
        return "hello , spring boot!";
    }
}

<aside> 🌷 可能遇到的问题:

</aside>