• SpringBoot的配置文件——.yml和.properties


    目录

    1. Spring Boot 配置文件的使用场景

    2. 配置文件的两种格式

    2.0 特殊说明:

    2.1 .properties 

    2.1.1 格式 

    2.2.2 缺陷 

    2.2.3 解决中文乱码的问题

    2.2  .yml

    2.2.3 格式

    配置数据库连接

    注意转义字符

    ​编辑 ​编辑

    配置null

    配置对象

     从.yml读取文件举例

    Student类及注意事项

    appliaction.yml

    Test

     配置集合示例

     创建不同环境下的配置文件


    1. Spring Boot 配置文件的使用场景

    1.系统配置文件:配置文件通常用于指定应用程序的全局设置,比如数据库连接、日志级别、端口号等。可以在配置文件中定义这些属性,并根据需要进行修改。

    2.用户配置文件:通过使用Spring Boot的配置文件,可以让用户在运行时指定他们希望覆盖的属性。例如,应用程序可能具有一些默认设置,但允许用户在配置文件中指定其他值

    2. 配置文件的两种格式

    .properties和.yml 

    2.0 特殊说明:

    1. 当.properties中设置端口号server.port=8888,而在.yml中设置

    server:

        port:6666,这个时候会以8888端口号!当一个项目中存在两种格式的配置文件,并且两个配置文件中设置了相同配置项,properties的优先级会更高

    2. 理论上这两种配置文件是可以共存的,但实际上我们会统一

    3. properties第一次用的话使用中文会乱码,但.yml不会,但是也可以通过别的方法来改变properties中文乱码的问题,下文中有介绍

    4.yml的通用性更好

    2.1 .properties 

    2.1.1 格式 

    #表示注释;key=value的形式
    1. server.port=8888
    2. mytest=zhangsan
    3. #连接数据库配置
    4. spring.datasource.url=jdbc:mysql://127.0.0.1:3306/testdb?characterEncoding=utf8
    5. spring.datasource.username=root
    6. spring.datasource.password=123456

    2.2.2 缺陷 

    mytest=张三,像这种使用中文会乱码,但是你也可以通过改成utf8的形式改变这个问题。

    2.2.3 解决中文乱码的问题

    1. @PropertySource
      @PropertySource(value="application.properties",encoding="utf-8")

       但是这种方式必须保证在application.properties , 中有

      1. @Value(value = "${my.value}", encoding = "utf-8")
      2. private String myValue;

    2.2  .yml

    2.2.3 格式

    1. server:
    2. port: 6666
    3. #自定义配置类
    4. mytest2: lisi

    这个必须值的前面有空格,这里的mytets2= 张三,写成中文也不会乱码。

    配置数据库连接

    1. # 配置数据库连接
    2. spring:
    3. datasource:
    4. url: jdbc:mysql://127.0.0.1:3306/testdb?characterEncoding=utf8
    5. username: root
    6. password: 123456

    对比一下:

    注意转义字符

     

    配置null

    1. # Null,~表示null,下行表示null是key,而value是一个特殊的值
    2. null.value: ~

    配置对象

    1. student:
    2. id: 1
    3. name: java
    4. age: 18
    5. # 或者就使用行内写法
    6. student2: {id: 1,name: java,age: 18}

     

     从.yml读取文件举例
    Student类及注意事项

    1. import lombok.Data;
    2. import org.springframework.boot.context.properties.ConfigurationProperties;
    3. import org.springframework.stereotype.Component;
    4. @Component
    5. @ConfigurationProperties("student")//将配置文件的属性值与一个特定的Java类库绑定起来
    6. @Data
    7. //使用lombok的@Data注解,里面就是我们想要的get,set,toString等方法
    8. public class Student {
    9. private int id;
    10. private String name;
    11. private int age;
    12. }

    注意事项 :

    •  Student类上方有@ConfigurationProperties("student")注解,并且里面写的必须与application.yml中的自定义配置对象名字一样
    • 实体类属性名要与配置中的key保持一致,并且提供settet和getter;则会就可以使用lombok中的@Data注解

    @Configuration注解就是把配置文件中的某个属性值与某个特定的Java类绑定起来。

    appliaction.yml

    1. # 配置对象
    2. student:
    3. id: 1
    4. name: java
    5. age: 18
    6. # 或者就使用行内写法
    7. student2: {id: 1,name: java,age: 18}
    Test

    最后的打印结果

     配置集合示例

    在 YAML 配置文件中,可以使用列表来定义一组值。

    1. students:
    2.   - id: 1
    3.     name: Alice
    4.     age: 18
    5.   - id: 2
    6.     name: Bob
    7.     age: 20
    8.   - id: 3
    9.     name: Charlie
    10.     age: 22

    在上面的配置中,我们定义了一个名为 `students` 的列表,其中包含了三个学生的信息,每个学生的信息又用一个 map 来表示。

    在 Spring Boot 中,可以通过 `@ConfigurationProperties` 注解来将 YAML 配置文件中的值注入到 Java 对象中。例如,我们可以创建一个 `Student` 类来表示一个学生的信息:

    1. @Data
    2. public class Student {
    3.     private int id;
    4.     private String name;
    5.     private int age;
    6. }

    然后,在 Spring Boot 应用程序中,可以使用以下方式将 `students` 列表中的所有元素注入到一个 `List` 对象中:

    1. @Component
    2. @ConfigurationProperties("students")
    3. @Data
    4. public class StudentConfig {
    5.     private List students;
    6. }

    在上面的代码中,我们使用 `@Component` 和 `@ConfigurationProperties` 注解将 `StudentConfig` 类声明为一个 Spring Bean,并将其与 YAML 配置文件中以 `students` 为前缀的配置项绑定起来。Spring Boot 会自动将 `students` 列表中的所有元素映射为 `List` 对象的属性值。

    现在,我们可以在其他组件中注入 `StudentConfig` 对象,并使用其中的 `students` 属性来获取所有学生的信息了。例如:

    1. ```java
    2. @Service
    3. public class StudentService {
    4.     private final StudentConfig studentConfig;
    5.     @Autowired
    6.     public StudentService(StudentConfig studentConfig) {
    7.         this.studentConfig = studentConfig;
    8.     }
    9.     public List getAllStudents() {
    10.         return studentConfig.getStudents();
    11.     }
    12. }
    13. ```

    在上面的代码中,我们注入了 `StudentConfig` 对象,并在 `getAllStudents()` 方法中返回了其中的 `students` 属性。

    这样,我们就可以轻松地将 YAML 配置文件中的列表注入到 Java 对象中,并在 Spring Boot 应用程序的其他组件中使用了。

     创建不同环境下的配置文件

    dev开发环境中可以设置端口号:1111

    prod中可以设置成别的端口号:2222

    在application.yml中可以去选择使用哪一种环境

    1. spring:
    2. profiles:
    3. active: dev

  • 相关阅读:
    SpringCloud gateway
    少儿编程 电子学会图形化 scratch编程等级考试四级真题答案解析(判断题)2022年9月
    04 使用Keil模拟器和Debug (printf) Viewer窗口实现scanf输入,并进行串口收发回环,无需fgetc重定向
    思维导图:定时器设计
    【JavaScript】JavaScript基础篇
    学会这3个小技巧,轻松去图片水印
    2. 使用IDEA创建Spring Boot Hello项目并管理依赖——Maven入门指南
    CV经典任务(二)目标检测 |单目标,多目标 非极大值抑制等
    ESP32编译出现Cannot establish a connection to the component registry.报错
    【算法入门&二叉树】从先中后序的遍历到用中后序列构造二叉树|如何抵挡递归法该死的魅力
  • 原文地址:https://blog.csdn.net/m0_74106420/article/details/133742210