Spring Boot的配置文件

配置文件的作用

整个项目中所有重要的数据都是在配置文件中配置,如数据库的连接信息,项目的启动端口,用于发现和定位问题的普通日志和异常日志等等。配置文件可以分为两类

  1. 系统使用的配置文件(系统配置文件),如端口号的设置,连接数据库的配置
  2. 用户自定义的配置文件

配置文件的格式

Spring Boot的配置文件可以分为 .properties和 .yml两种格式。.properties属于早期时代的格式,也是Spring Boot项目的默认配置文件。当一个项目中存在两种格式的配置文件,并且两个配置文件都设置了相同的配置项,但值不同,那么properties的优先级更高。通常在一个项目中只会存在一种格式的配置文件。

properties的用法

  1. properties是以键值的形式配置的,key和value之间用"="连接,中间没有空格。
properties 复制代码
# 系统配置端口号
server.port=8888
# 自定义配置
name=zhangsan
  1. 使用@Value注解使用${}的格式读取配置文件
java 复制代码
@RestController
	public class Controller {
		@Value("${name}")//要和配置文件中的key值相同
		private String name;
		@PostConstruct
		public void sayHi() {
			System.out.println("hi: " + name);
		}
	}

:::info

  1. propertices的缺点
    :::

    我们发现properties有很多冗余信息,而使用yml就可以很好的解决

yml配置文件

yml的优点

  1. yml写法简单,可读性高
  2. yml支持更多数据类型,如数组,对象
  3. yml支持多语言

稍微规模大点的公司都开始使用微服务,像字节内部有java,go,python,php语言,只关心业务是否能够实现,使用什么语言并不关心。如果使用properties配置文件就要写多份,而yml就很好的解决了这个问题。

yaml 复制代码
key: value

注意:key和value之间使用冒号和空格组成

yml版本的连接数据库

yml进阶
配置不同数据类型
yaml 复制代码
#字符串
string.value: hello
#布尔值
boolean.value: true
boolean.value2: false
#整数
int.value: 10
#浮点数
float.value: 3.14
#空值,~表示Null
null.value: ~

读取配置文件中的基础类型使用@Value("${}")注解

java 复制代码
@RestController
	public class Controller {
		//要和key值对应
		@Value("${string.value}")
		private String hello;
		@PostConstruct
		public void postConstruct() {
			System.out.println(hello);
		}
		@Value("${boolean.value}")
		private boolean bool;
		@PostConstruct
		public void postConstruct2() {
			System.out.println(bool);
		}
		@Value("${null.value}")
		private Integer integer;
		@PostConstruct
		public void postConstruct3() {
			System.out.println(integer);
		}
	}

:::success

注意:value值加单/双引号

:::

yaml 复制代码
string:
  str1: hello \n Spring Boot
  str2: 'hello \n Spring Boot'
  str3: "hello \n Spring Boot"
java 复制代码
@RestController
public class Controller {
    @Value("${string.str1}")
    private String str1;
    @PostConstruct
    public void construct1() {
        System.out.println("str1: " + str1);
    }
    @Value("${string.str2}")
    private String str2;
    @PostConstruct
    public void construct2() {
        System.out.println("str2: " + str2);
    }
    @Value("${string.str3}")
    private String str3;
    @PostConstruct
    public void construct3() {
        System.out.println("str3: " + str3);
    }
}

由上可知

  1. 字符串默认不用加上单引号或者双引号
  2. 单引号会转义特殊字符,特殊字符最终只是一个普通的字符串数据
  3. 双引号不会转义字符串里面的特殊字符,特殊字符会作为本身想表示的意思
配置对象
yaml 复制代码
#自定义一个对象,两种写法
#student:
#  id: 1
#  name: zhangsan
#  age: 18
student: {id: 1, name: zhangsan, age: 18}

读取配置文件中的对象使用@ConfigurationProperties注解

java 复制代码
@Component
//三种写法
//@ConfigurationProperties(value = "student")
//@ConfigurationProperties(prefix = "student")
@ConfigurationProperties("student")
@Data //需要提供get,set方法才能够把配置文件的信息读取出来
public class Student {
    //类型和名字要一一对应
    private int id;
    private String name;
    private int age;
}
配置集合
yaml 复制代码
#自定义集合
#list:
#  array:
#    - 1
#    - 2
#    - 3
list: {array: [1,2,3]}

读取配置文件中的集合使用@ConfigurationProperties

java 复制代码
@Component
	@ConfigurationProperties("list")
	@Data
	public class MyList {
		private List<Integer> array;
	}

properties和yml的区别

  1. properties是以key=value的形式进行配置,而yml是使用类json格式。
  2. properties为早期且默认的配置文件格式,其配置存在一定的冗余数据,使用yml可以很好的解决数据冗余的问题
  3. yml通用性更好,支持多种语言,例如开发一个云服务器,可以使用同一份配置文件作为java和go的共同配置文件
  4. yml支持更多的数据类型

设置不同环境的配置文件

在一个项目中有多种环境,如开发环境,测试环境,生产环境。每个环境的配置项都有所不同,如何让一个配置文件适应不同的环境呢?

把配置文件设为生产环境

yaml 复制代码
spring:
  profiles:
    active: prod
yaml 复制代码
server:
  port: 9999
yaml 复制代码
server:
  port: 7777

此时使用的就是生产环境配置的端口号

相关推荐
你的保护色7 小时前
多AS互联BGP4综合实验设计
java
某不知名網友8 小时前
ROS2进阶:服务通信、自定义SRV接口、参数服务器与基于服务的参数通信详解
java·开发语言·microsoft
Wang's Blog8 小时前
Java 项目实战: 交友项目 - MongoDB 入门:体系结构和命令行操作
java·mongodb·交友
小溪学编程8 小时前
将 Bean 放入 Spring 容器中的方式有哪些?
java·后端·spring
java_nnnn8 小时前
LeetCode刷题滑动窗口(水果成篮+找到字符串中所有字母的异位词)Java
java·开发语言·leetcode
用户3721574261358 小时前
Java 读取、修改和清空 PDF 文档属性(元数据)
java
SL_staff8 小时前
从‘改一行代码等三天’到‘改完即生效’:实施工程师的逻辑引擎避坑手记
java·产品·全栈
小羊没烦恼!8 小时前
异步编程:线程概述及使用
java·开发语言·mysql·算法·c#
hhwyqwqhhwy8 小时前
设备树之 provider 和 consumer
java·linux·前端
\光辉岁月/8 小时前
9.日期时间类
java·date·localdatetime·日期类