📲目录

一、为什么要配置文件,配置文件的作用?

二、SpringBoot配置文件的方法

二、SpringBoot配置文件的格式

一、properties

二、yml

三、读取配置文件

1、properties

2、yml

3、配置对象


一、为什么要配置文件,配置文件的作用?


🍧因为:整个项目中所有重要的数据都是在配置⽂件中配置的,⽐如:


🚅1、数据库的连接信息(包含⽤户名和密码的设置);


🚄2、项目的启动端口;


🚂3、第三方系统的调⽤秘钥等信息;


🚝4、用于发现和定位问题的普通⽇志和异常⽇志等。


🛰想象⼀下如果没有配置信息,那么 Spring Boot 项目就不能连接和操作数据库,甚⾄是不能保存可以用于排查问题的关键⽇志,所以配置⽂件的作用是⾮常重要的;




springboot配置so库 springboot 数据库配置文件_配置文件


二、SpringBoot配置文件的方法

1、系统使用的配置文件,如端口号的设置,连接数据库的配置。

2、用户自定义的配置文件。

二、SpringBoot配置文件的格式

一、properties


语法:properties 是以键值的形式配置的,key 和 value 之间是以“=”连接的;



1、当我们没有设置配置文件的时候,我们的SpringBoot默认启动的端口号是8080;

springboot配置so库 springboot 数据库配置文件_配置文件_02

2 、当我们通过properties来设置一个端口号时,我们启动springboot项目来观察一下;

2.1在resources目录下新建一个文件applications.properties;

springboot配置so库 springboot 数据库配置文件_java_03

2.2这里我们可以自己设置任意一个端口号,比如我们给个8888;

server.port=8888

再次运行观察结果:

springboot配置so库 springboot 数据库配置文件_springboot配置so库_04

结果可以看到,我们自己设置的端口号生效了;

那么如何访问到呢?

我们之前在demo包下面新建了一个TestController类,内容如下:

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller//当前类是控制器
@ResponseBody//返回的是数据,而非页面
public class TestController {

    @RequestMapping("/hello")//url路由注册
    public String sayHi(String name){

        if(!StringUtils.hasLength(name)){
            name = "SpringBoot";
        }
        return "hello"+ name;
    }
}

这个时候我们就可以通过浏览器来访问,在浏览器中输入以下内容:

http://localhost:8888/hello

按下回车,观察运行结果:

springboot配置so库 springboot 数据库配置文件_spring_05

二、yml

1、yml 是 YAML 是缩写,它的全称 Yet Another Markup Language 翻译成中⽂就是 “另⼀种标记语言”。

yml 是树形结构的配置文件,它的基础语法是“key: value”,注意 key 和 value 之间使⽤英⽂冒汗加空 格的方式组成的,其中的空格不可省略;

2、首先还是在resources目录下新建一个yml文件,格式如下:

springboot配置so库 springboot 数据库配置文件_springboot配置so库_06

yml文件代码如下,我们来测试一下新设置的端口号是否生效;

springboot配置so库 springboot 数据库配置文件_配置文件_07

3、在浏览器输入:http://localhost:7777/hello 按下回车,看运行结果;

springboot配置so库 springboot 数据库配置文件_配置文件_08

结果依然是没有问题;

注:理论上讲 properties 可以和 yml ⼀起存在于⼀个项⽬当中,当 properties 和 yml ⼀起存在⼀个项目中时,如果配置⽂件中出现了同样的配置,比如 properties 和 yml 中都配置“server.port”, 那么这个时候会以 properties 中的配置为主,也就是 .properties 配置⽂件的优先级最高,但加载完 .properties ⽂件之后,也会加载 .yml 文件的配置信息。

三、读取配置文件

1、properties


如果在项⽬中,想要主动的读取配置文件中的内容,可以使用 @Value 注解来实现。


@Value 注解使用“${}”的格式读取;



application.yml文件代码如下:


server:
  port: 7777
  #自定义配置项
mytest: 小黑

TestController:

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller//当前类是控制器
@ResponseBody//返回的是数据,而非页面
public class TestController {

    @Value("${mytest}")
    private String mytest;


    @RequestMapping("/hello")//url路由注册
    public String sayHi(String name){

        if(!StringUtils.hasLength(name)){
            name = "SpringBoot";
        }
        return "hello"+ name;
    }

    @RequestMapping("/getconf")
    public String getconf(){
        return mytest;
    }
}

首先启动springboot类,启动成功后,再去浏览器访问对应的地址;

springboot配置so库 springboot 数据库配置文件_springboot配置so库_09

浏览器输入以下内容:http://localhost:7777/getconf  按下回车:

springboot配置so库 springboot 数据库配置文件_spring_10

2、yml

2.1 value值加单双引号;

application.yml文件写入以下内容:

#字符串
myStirng1: 哈喽\n呀
myStirng2: '哈喽\n呀'
myStirng3: "哈喽\n呀"

TestController类中代码:

@Value("${myString1}")
    private String myString1;

    @Value("${myString2}")
    private String myString2;

    @Value("${myString3}")
    private String myString3;

    @PostConstruct
    public void PostConstruct(){
        System.out.println("myString1"+myString1);
        System.out.println("myString2"+myString2);
        System.out.println("myString3"+myString3);
    }

启动springboot项目,观察结果;

得以下结论:

1、字符串默认不用加上单引号或者双引号。

2、单引号不会转义特殊字符,特殊字符最终只是⼀个普通的字符串数据。


3、双引号会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思。



3、配置对象

1、我们还可以在yml中配置对象,如下格式:

student:
  id: 1
  name: zhangsan
  age: 18

还可以使用行内写法,将对象中的属性写到一行,不用另起很多行;

student1: {id: 1,name: zhangsan,age: 20}

那么读取对象的话就不能使用注解@Value了,需要用到另外一个注解@ConfigurationProperties 来读取;

在demo包下新建一个类Student:

package com.example.demo;

import lombok.Data;
import lombok.NonNull;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("student")
@Data
public class Student {
    private int id;
    @NonNull
    private String name;
    private int age;

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Student(int id, @NonNull String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

TestController中代码:

@Component
    public class ReadYml2 {
        @Autowired
        private Student student;
        @PostConstruct
        public void postConstruct() {
            System.out.println(student);
        }
    }

运行结果:

springboot配置so库 springboot 数据库配置文件_java_11

⏳注意:1、学生类Student使用@ConfigurationProperties("student")这个注解,并且注解后面的对象名必须和配置文件中的一致;

⌛️2、实体类的属性名必须和配置中的key保持一致,并且提供getter和setter方法;