[转帖]Spring Boot 配置文件相关操作

spring,boot,配置文件,相关,操作 · 浏览次数 : 0

小编点评

**配置加载顺序:** 1. 默认配置文件:`application.yml` 2. 开发环境配置文件:`application-dev.properties` 3. 测试环境配置文件:`application-test.properties` 4. 生产环境配置文件:`application-pro.properties` 5. 外部配置文件(优先级最高):`application.properties`或`/config/application.properties` **常见外部配置文件的加载:** * `application.properties`:位于项目根目录下的 `config` 目录下 * `application-dev.properties`:位于开发环境目录下的 `config` 目录下 * `application-test.properties`:位于测试环境目录下的 `config` 目录下 * `application-pro.properties`:位于生产环境目录下的 `config` 目录下 **其他配置加载提示:** * 使用 `@Value` 注解读取配置参数。 * 使用 `@ConfigurationProperties` 注解读取配置属性。 * 使用 `@Autowired` 注解对配置对象进行注入。 * 使用 `@Component` 注解标记类为配置类。 * 使用 `@PropertySource` 注解加载属性从属性文件中加载。

正文

https://zhuanlan.zhihu.com/p/537590504

 

  • 默认配置文件名称: application
  • 常用后缀:propertiesymlyaml
  • 优先级: properties > yml > yaml

1. Spring Boot 读取配置文件

假设配置文件 application.yml 内容如下

library: ZeroLibrary

borrower:
    name: cc01cc
    limit: 5

book:
    - ZeroBook
    - OneBook

1.1. 使用 @Value 读取配置

// Value 中的值需要和配置文件的键一致
// 变量名的选取和配置文件直接关系
@Value("$(library)")
public String library; // library = "ZeroLibrary"

@Value("$(borrower.name")
public String borrowerName; // borrowerName = "cc01cc"

@Value("$(book[0]")
public String zeroBook; // zeroBook = "ZeroBook"

*不利于获取大量的值

1.2. 使用 Environment 读取配置

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

@Autowired
private Environment env;

String library = env.getProperty("library");
String borrowerName = env.getProperty("borrow.name");
String zeroBook = env.getProperty("book[0]");

1.3. 使用 @ConfigurationProperties 读取配置

*配置和类绑定

1.3.1. 添加依赖

Configuration Metadata: 
<!-- 在写配置文件的时候提供提示 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

1.3.2. 代码示例

创建对象类 Borrower

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
// 前缀用于指定下方变量所对应键的层级归属
@ConfigurationProperties(prefix = "borrower")
public class Borrower {
    // 变量名需要和配置文件中的键一致
    // 类名和配置文件中的键无直接关系
    private String name;
    private int limit;
}

2. Spring Boot 动态切换配置

原因:不同场景(开发,测试,生产)使用的配置文件不同,如:数据库地址,服务器端口等,每次打包修改配置文件会相对繁琐

2.1. 配置文件

2.1.1. 使用多个配置文件的方式

resources
│
├─ application.properties // 默认配置文件
│
├─ application-dev.properties // 添加后缀,自定义配置文件(开发环境,常用dev)
│
├─ application-test.properties // test 常用于测试环境
│
└─ application-pro.properties // pro 常用于生产环境

2.1.2. 使用单个 yml 文件(properties 文件不适用)

使用 --- 分隔配置文件

并使用 spring.config.activate.on-profile 指定每个配置文件(2.4 版本之前是 spring.profiles)

示例

---
server:
  port: 8081

spring:
  config:
    activate:
      on-profile: dev
---
server:
  port: 8082

spring:
  config:
    activate:
      on-profile: test
---

2.2. 激活配置文件

2.2.1. 使用配置文件激活

使用 spring.profiles.active 激活配置文件

示例

# 在默认配置文件 application 中使用
spring.profiles.active=dev

2.2.2. 使用虚拟机参数激活

虚拟机参数(VM options)添加 -Dspring.profiles.active=dev

  • -D 开头,后接参数和值
  • 会覆盖配置文件参数

2.2.3. 使用命令行参数激活

命令行参数(Program arguments)添加 --spring.profiles.active=dev

  • -- 开头,后接参数和值
  • 会覆盖配置文件参数

示例

java -jar xxx.jar --spring.profiles.active=dev

3. 内部配置文件的加载顺序

依次从以下位置加载配置文件(由上往下,优先级由高到低)

file: ./config/   当前项目的 /config 目录下
file: ./          当前项目的根目录下
classpath:/config/   classpath 的 /config 目录下
classpath:/          classpath 的根目录下
  1. Maven 中 resources 和 java 目录都会打包入 classpath 根目录
  2. 项目根目录的文件默认不会被打包

4. 常用外部配置文件的加载

Spring Boot Reference Documentation - features.external-config: 
  1. 使用 spring.config.loaction 参数加载外部配置文件
  2. 在打包后的 jar 包 的同级目录放 application.properties 或 /config/application.properties 配置文件(优先级高于包内配置文件)

5. 参考

  1. 黑马程序员SpringBoot教程_哔哩哔哩_bilibili 

与[转帖]Spring Boot 配置文件相关操作相似的内容:

[转帖]Spring Boot 配置文件相关操作

https://zhuanlan.zhihu.com/p/537590504 默认配置文件名称: application 常用后缀:propertiesymlyaml 优先级: properties > yml > yaml 1. Spring Boot 读取配置文件 假设配置文件 applicat

[转帖]SpringBoot配置SSL 坑点总结【密码验证失败、连接不安全】

文章目录 前言1.证书绑定问题2.证书和密码不匹配3.yaml配置文件问题3.1 解密类型和证书类型是相关的3.2 配置文件参数混淆 后记 前言 在SpringBoot服务中配置ssl,无非就是下载证书设置一下配置文件的问题,这里主要记录我在配置的过程中遇到的坑点。 如果是新手上道的话建议结合其他的

[转帖]springcloud nacos配置

配置文件中的nacos配置,discovery和config配置项 版本: 2.3.2.RELEASE Hoxton.SR9

[转帖]springboot指定端口的三种方式

https://blog.51cto.com/feirenraoyuan/5504099 第一配置文件中添加server.port=9090 第二在命令行中指定启动端口,比如传入参数 java -jar bootsample. jar -- server.port=9000 第三传入虚拟机系统属性

[转帖]Spring Boot 3 Ships November 2022, Delays Java Module Support

Spring Boot 3 Ships November 2022, Delays Java Module Supporthttps://www.infoq.com/news/2022/10/spring-boot-3-jax-london/ Join a community of experts.

[转帖]Spring Boot中Tomcat是怎么启动的

https://zhuanlan.zhihu.com/p/208318177 Spring Boot一个非常突出的优点就是不需要我们额外再部署Servlet容器,它内置了多种容器的支持。我们可以通过配置来指定我们需要的容器。 本文以我们平时最常使用的容器Tomcat为列来介绍以下两个知识点: Spr

[转帖]Spring Boot 依赖包及作用

目录 作者:@dwtfukgv本文为作者原创,转载请注明出处:https://www.cnblogs.com/dwtfukgv/articles/10179922.html Spring Boot 之Spring Boot Starter依赖包及作用 spring-boot-starter这是Spr

[转帖]记录一次spring-boot程序内存泄露排查

现象 spring boot项目jvm启动配置-Xms4g -Xmx4g,然而很不幸的是程序所占的内存越来越高,都达到了12个多G,只能临时重启服务 常用命令 jstat -class PIDjstat -compiler PIDjstat -gc PIDjstat -gccapacity PIDj

[转帖]Prometheus + Spring Boot 应用监控

https://blog.51cto.com/u_15127622/2757942 1. Prometheus是什么Prometheus是一个具有活跃生态系统的开源系统监控和告警工具包。一言以蔽之,它是一套开源监控解决方案。Prometheus主要特性:多维数据模型,其中包含由指标名称和键/值对标识

[转帖]7 种提升 Spring Boot 吞吐量神技!

https://cloud.tencent.com/developer/article/2045348?areaSource=105001.6&traceId=7RuArY2Tm1MQWwQaMnx-Q 一、异步执行 实现方式二种: 1、 使用异步注解@aysnc、启动类:添加@EnableAsyn