侧边栏壁纸
博主头像
快乐江湖的博客博主等级

更多内容请点击CSDN关注“快乐江湖”

  • 累计撰写 127 篇文章
  • 累计创建 33 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

第六章:SpringBoot配置文件

快乐江湖
2023-11-23 / 0 评论 / 0 点赞 / 5 阅读 / 23685 字

一:配置文件概述

(1)配置文件作用

SpringBoot配置文件作用:Spring Boot 的配置文件在应用程序中起着关键的作用,它用于配置应用程序的各种属性、行为和第三方服务的连接信息。通过配置文件,开发者可以在不修改代码的情况下改变应用程序的行为,使得应用程序更加灵活和可配置。主要作用如下

  • 配置应用程序属性:配置文件中可以定义各种属性,这些属性可以是数据库连接信息、服务器端口、日志级别等。这些属性的值可以在整个应用程序中使用,通过这种方式实现了应用程序的参数化配置

  • 配置文件优先级:Spring Boot 配置文件具有优先级顺序,系统会按照一定的规则来加载配置文件。例如,application.properties 的配置项会被默认加载,而同样的配置项在其他配置文件中也可以被覆盖。这种优先级机制使得配置更加灵活

  • 多环境配置:Spring Boot 支持在不同的环境下使用不同的配置文件。例如,可以有一个 application-dev.properties 用于开发环境,application-prod.properties 用于生产环境。通过设置不同的配置文件,可以方便地切换不同环境下的配置

  • 外部配置:Spring Boot 支持在外部指定配置文件的位置,可以通过命令行参数、环境变量或者其他方式指定配置文件的路径,从而实现更好的配置管理

  • 配置注解:除了配置文件,Spring Boot 还支持使用注解来进行配置。通过在代码中使用 @Value@ConfigurationProperties 等注解,可以将配置文件中的属性值注入到相应的 Java 类中,实现更灵活的配置

(2)配置文件分类

SpringBoot配置文件分类

  • 系统配置文件 例如端口号的设置,连接数据的配置
  • 用户自定义配置文件

(3)配置文件格式

SpringBoot配置文件格式:主要分为以下两种格式

  • Properties 格式 是一种键值对的文本文件格式,常用于配置文件。在 Spring Boot 中,通常使用 .properties 扩展名的文件
  • YAML格式 是一种人类可读的数据序列化格式,常用于配置文件。在 Spring Boot 中,通常使用 .yml 或 .yaml 扩展名的文件

.properties文件和.yml文件是可以共存于一个项目当中的。如果同时存在两个配置文件,就会优先选用.properties文件(加载完之后仍然会加载.yml文件配置信息)。虽然两者可以共存,但是在实际业务中,我们一般会采取统一的配置文件格式

二:properties配置文件说明

(1)基本语法

基本语法

  • 语法key=value
  • 注释#
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
logging.level.root=debug

(2)读取配置文件

在项目中读取配置文件可以使用@Value注解,格式为@Value("${}")

test="zhangsan"
package com.example.springbootfirst2023_11_12;  
  
import org.springframework.beans.factory.annotation.Value;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.ResponseBody;  
  
@Controller  
@ResponseBody  
public class TestController {  
    @Value("${test}")  
    private String mytest;  
  
    @RequestMapping("/getconfig")  
    public String getConf(){  
        return mytest;  
    }  
  
}

(3)缺点

.properties缺点

  • 缺乏层次结构: .properties 文件是平面的键值对,不支持层次结构。这意味着在处理复杂的配置需求时,你可能需要使用多个键值对来模拟层次结构,导致配置文件变得复杂和难以维护
  • 不支持复杂的数据类型: .properties 文件中的值都是字符串,不支持复杂的数据类型。当你需要配置复杂的对象或集合时,可能需要手动进行类型转换,增加了额外的代码复杂性
  • 不支持动态刷新: 在运行时更新 .properties 文件的更改并不会自动反映到应用程序中,除非你使用了Spring Boot的@RefreshScope等特性。这可能导致在进行配置更改时需要重启应用程序
  • 无法进行条件化配置: 使用 .properties 文件时,难以根据条件来配置不同的属性。Spring Boot 2.4及更高版本引入了更灵活的配置方式,如使用 YAML 文件,并且支持基于条件的配置

三:yml配置文件说明

yml:是YAML的缩写,全称为"Yet Another Markup language",是一种标记语言,有以下优点

  • 语言可读性高、写法简单、易于理解,语法和JSON类似
  • 支撑更多的数据类型,可以简单表达数组、散列表、标量等数据心态
  • 使用空白符号缩进和大量依赖外观的特色
  • 支撑更多的编程语言

(1)基本语法

基本语法

  • 语法key: value,注意:后面必须有一个空格
  • 注释 #
server:  
  port: 8080  
spring:  
  datasource:  
    url: jdbc:mysql://localhost:3306/mydatabase  
  profiles:  
    active: dev

(2)读取配置文件

任然使用@Value("${}")读取

test: "lisi"
package com.example.springbootfirst2023_11_12;  
  
import org.springframework.beans.factory.annotation.Value;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.ResponseBody;  
  
@Controller  
@ResponseBody  
public class TestController {  
    @Value("${test}")  
    private String mytest;  
  
    @RequestMapping("/getconfig")  
    public String getConf(){  
        return mytest;  
    }  
  
}

(3)使用进阶

A:配置其他数据类型

如下

# 字符串
string.value: Hello

# 布尔值
boolean.value: true
boolean.value1: false

# 整数
int.value: 10
int.value1: 0b # 二进制

# 浮点数
float.value: 3.14159
float.value1: 3.14159e-5

# Null
null.value: ~

B:配置对象

在yml中可以使用如下两种方式配置对象

# 方式一
student:
	id: 1
	name: "zhangsan"
	age: 18
# 方式二
student: {id: 1,name: "zhangsan", age:18}
	

这时候就需要另外一个注解@ConfigurationProperties进行读取

1:创建一个实体类,并加入注解

package com.example.springbootfirst2023_11_12;  
  
import lombok.Getter;  
import lombok.Setter;  
import lombok.ToString;  
import org.springframework.boot.context.properties.ConfigurationProperties;  
import org.springframework.stereotype.Component;  
  
@Component  
@ConfigurationProperties("student")  
@Getter  
@Setter  
@ToString  
public class Student {  
    private int id;  
    private String name;  
    private int age;  
}

2:进行调用

package com.example.springbootfirst2023_11_12;  
  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.beans.factory.annotation.Value;  
import org.springframework.boot.context.properties.ConfigurationProperties;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.ResponseBody;  
  
@Controller  
@ResponseBody  
public class TestController {  
    @Autowired  
    private Student student;  
  
    @RequestMapping("/getconfig")  
    public void getConf(){  
        System.out.println(student);  
    }  
  
}

3:效果

C:配置集合

方法如下

# 方式一
dtypes:   
  name:   
    - mysql  
    - sqlserver  
    - db2

# 方式二
dbtypes: {name: [mysql,sqlserver,db2]}

读取方式如下

1:创建一个实体类,并加入注解

package com.example.springbootfirst2023_11_12;  
  
import lombok.Data;  
import org.springframework.boot.context.properties.ConfigurationProperties;  
import org.springframework.stereotype.Component;  
  
import java.util.List;  
  
@Component  
@ConfigurationProperties("dbtypes")  
@Data  
public class ListTest {  
    private List<String> name;  
}

2:进行调用

package com.example.springbootfirst2023_11_12;  
  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.beans.factory.annotation.Value;  
import org.springframework.boot.context.properties.ConfigurationProperties;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.ResponseBody;  
  
@Controller  
@ResponseBody  
public class TestController {  
    @Autowired  
    private ListTest listTest;  
  
    @RequestMapping("/getconfig")  
    public void getConf(){  
        System.out.println(listTest);  
    }  
  
}

3:效果

四:其他

(1)设置不同环境的配置文件

一般来说,开发环境、生产环境的配置信息是不同的,所以我们要创建属于不同环境下配置文件,如下

使用时在主配置文件(appliaction.yml)指定具体环境即可

(2)其他配置项

更多配置项请见官方文档:点击跳转

(3)SpringBoot读取配置文件的几种方式

具体请见该博客:点击跳转

0

评论区