当前位置: 首页 > news >正文

Spring Boot 中加载多个 YAML 配置文件

在 Spring Boot 中加载多个 YAML 配置文件是一个常见的需求,通常用于将配置信息分离到多个文件中以便于管理和维护。Spring Boot 提供了灵活的方式来加载多个 YAML 配置文件。

以下是一些方法和步骤,用于在 Spring Boot 应用中加载多个 YAML 配置文件:

方法一:使用 spring.config.import 属性

Spring Boot 2.4 及以上版本引入了 spring.config.import 属性,使得加载多个配置文件变得更加方便。你可以在 application.ymlapplication.properties 文件中使用 spring.config.import 来引入其他 YAML 文件。

例如,假设你有以下两个 YAML 文件:

  • application.yml
  • config-part1.yml
  • config-part2.yml

你可以在 application.yml 中这样配置:

spring:config:import:- classpath:config-part1.yml- classpath:config-part2.yml

方法二:使用 spring.profiles.activespring.profiles.include

通过配置不同的 profiles,你可以在不同的环境下加载不同的配置文件。假设你有以下文件:

  • application.yml
  • application-dev.yml
  • application-prod.yml

你可以在 application.yml 中定义一些通用配置,然后在 application-dev.ymlapplication-prod.yml 中定义特定环境的配置。

例如,application.yml

server:port: 8080

application-dev.yml

spring:datasource:url: jdbc:mysql://localhost:3306/devdbusername: devuserpassword: devpassword

application-prod.yml

spring:datasource:url: jdbc:mysql://prod-db-server:3306/proddbusername: produserpassword: prodpassword

然后,你可以通过命令行参数或环境变量来指定活动的 profile,例如:

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

或者,使用 spring.profiles.include 在一个 profile 文件中包含其他 profile 文件:

# application-full.yml
spring:profiles:include: dev,custom

方法三:在 application.properties 中指定配置文件位置

你还可以在 application.properties 文件中通过 spring.config.location 属性指定 YAML 文件的位置。

例如:

spring.config.location=classpath:/default.yml,classpath:/override.yml

方法四:使用 @ConfigurationProperties@PropertySource

虽然这不是直接加载多个 YAML 文件的方法,但你可以将 YAML 文件转换为 properties 文件,并使用 @PropertySource 注解来加载它们。然后,你可以使用 @ConfigurationProperties 将这些属性绑定到一个配置类。

例如,创建一个 custom.properties 文件:

custom.property1=value1
custom.property2=value2

然后,在你的配置类中使用 @PropertySource@ConfigurationProperties

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;@Configuration
@PropertySource("classpath:custom.properties")
@ConfigurationProperties(prefix = "custom")
public class CustomProperties {private String property1;private String property2;// getters and setters
}

总结

Spring Boot 提供了多种方法来加载多个 YAML 配置文件,选择哪种方法取决于你的具体需求和应用场景。无论是使用 spring.config.import、profiles、spring.config.location 还是 @PropertySource,都可以帮助你有效地管理和加载配置信息。

http://www.lryc.cn/news/535016.html

相关文章:

  • 厚植创新实力、聚焦生物科技:柏强制药的责任与机遇
  • Linux中getifaddrs函数
  • 【HarmonyOS Next 自定义可拖拽image】
  • 解决No module named ‘llama_index.llms.huggingface‘
  • SearchBar组件的功能与用法
  • 13.推荐系统的性能优化
  • Grafana-使用Button修改MySQL数据库
  • 飞科FH6218电吹风异响维修
  • 分治下的快速排序(典型算法思想)—— OJ例题算法解析思路
  • Unity3D实现显示模型线框(shader)
  • 深度剖析责任链模式
  • 基于 openEuler 构建 LVS-DR 群集
  • CSS3+动画
  • 使用DeepSeek和Kimi快速自动生成PPT
  • DeepSeek使用最佳实践
  • 机器学习 - 进一步理解最大似然估计和高斯分布的关系
  • Oracle常用导元数据方法
  • linux安装jdk 许可证确认 user did not accept the oracle-license-v1-1 license
  • Spring基于文心一言API使用的大模型
  • 【Elasticsearch】derivative聚合
  • 4.7.KMP算法(新版)
  • iOS AES/CBC/CTR加解密以及AES-CMAC
  • 错误报告:WebSocket 设备连接断开处理问题
  • 点云配准网络
  • 黑马Redis详细笔记(实战篇---短信登录)
  • 51单片机俄罗斯方块整行消除函数
  • Vue 3 30天精进之旅:Day 21 - 项目实践:打造功能完备的Todo应用
  • 32单片机学习记录1之GPIO
  • AI 编程助手 Cline
  • YOLOv11-ultralytics-8.3.67部分代码阅读笔记-patches.py