spring boot 配置多个yaml文件并根据env参数加载

     2023-02-26     263

关键词:

【中文标题】spring boot 配置多个yaml文件并根据env参数加载【英文标题】:springboot configuring mutiple yaml files and load them based on env param 【发布时间】:2021-06-03 06:21:45 【问题描述】:

我必须调整一个 springboot 应用程序,该应用程序具有配置应用程序属性的 application.yaml 文件。

使用java类文件中的注解映射属性以读取值@ConfigurationProperties(prefix="propName")

现在的要求是我需要另一个名为 application-profile-2.yml 的文件并将 application.yml 重命名为 application-profile-1.yml,这些文件需要由参数 -Drun-profile 获取=profile1 当我运行/构建应用程序时

当我在构建/运行命令期间传递参数 -Drun-profile=profile2 时,如何动态地让我的应用加载 application-profile-2.yml?

【问题讨论】:

你的问题到底是什么? 抱歉,刚刚意识到我输入了一半,我再次编辑了我的问题,希望现在有意义:) 结帐mkyong.com/spring-boot/… 【参考方案1】:

您可以在单个 yaml 文件中执行此操作。 Yaml 允许您继承和覆盖部分。 profile2 的属性将覆盖usernamepassword,但继承url

spring:
  datasource:
    url: jdbc:postgresql://localhost/mydb
    username: username
    password: password

---

spring:
  profiles: profile2
  datasource:
    username: username2
    password: password2

【讨论】:

但是拥有多个 yml 文件的方式是什么? application-profileName.yml 是您正在寻找的。 docs.spring.io/spring-boot/docs/current/reference/html/…

Spring Boot 无法从配置文件特定的 yaml 文件中读取

】SpringBoot无法从配置文件特定的yaml文件中读取【英文标题】:Springbootunabletoreadfromprofilespecificyamlfile【发布时间】:2018-03-0506:15:34【问题描述】:我在玩springboot并试图创建一个配置文件特定的配置文件。我叫它application-local.yml... 查看详情

如何使用 YAML 文件在 Spring Boot 中配置 Swagger?

】如何使用YAML文件在SpringBoot中配置Swagger?【英文标题】:HowtoconfigureSwaggerinSpringBootusingYAMLfile?【发布时间】:2018-11-2912:41:31【问题描述】:我的Swagger配置有问题。现在Swagger正在扫描包并创建漂亮的文档,但我想使用我的YAML(... 查看详情

Spring Boot YAML 配置和列表

】SpringBootYAML配置和列表【英文标题】:SpringBootYAMLconfigandlist【发布时间】:2016-01-1715:17:23【问题描述】:我必须在SpringBoot的YAML配置文件中集成一个列表,但不知道如何继续。我已经看到了其他相关的问题:SpringBootyamlconfiguratio... 查看详情

如何使用 spring boot + .yaml 创建配置文件?

】如何使用springboot+.yaml创建配置文件?【英文标题】:HowcanIcreateprofileswithspringboot+.yaml?【发布时间】:2019-03-0816:23:45【问题描述】:我有带有2个属性文件的SpringBoot服务器:application-local.properties和application-test.properties在每个文... 查看详情

如何使用 spring boot + .yaml 创建配置文件?

】如何使用springboot+.yaml创建配置文件?【英文标题】:HowcanIcreateprofileswithspringboot+.yaml?【发布时间】:2019-03-0816:23:45【问题描述】:我有带有2个属性文件的SpringBoot服务器:application-local.properties和application-test.properties在每个文... 查看详情

多个 Spring 配置文件的属性解析(yaml 配置)

】多个Spring配置文件的属性解析(yaml配置)【英文标题】:PropertyresolvingformultipleSpringprofiles(yamlconfiguration)【发布时间】:2020-06-0513:16:33【问题描述】:当多个配置文件用于属性解析时是否有定义的顺序。我有yaml配置文件:name:n... 查看详情

如何在 Spring Boot Yaml 中激活 Profile

】如何在SpringBootYaml中激活Profile【英文标题】:HowtoactivateProfileinSpringBootYaml【发布时间】:2021-10-1521:51:05【问题描述】:我的Springboot(2.4.5)应用程序的以下应用程序yaml有两个配置文件。弹簧:个人资料:开发人员弹簧:个人资料... 查看详情

Spring Boot Mongodb .yaml 配置

】SpringBootMongodb.yaml配置【英文标题】:SpringbootMongoDB.ymlconfiguration【发布时间】:2019-10-1517:24:21【问题描述】:当我使用MySQL和hibernate进行SpringBoot时,我在.yml文件中使用以下配置spring:datasource:url:jdbc:mysql://localhost/userName?zeroDateTime... 查看详情

使用 Gradle 管理 Spring Boot 配置文件

】使用Gradle管理SpringBoot配置文件【英文标题】:ManageSpringBootProfileswithGradle【发布时间】:2021-05-0900:57:46【问题描述】:我有一个由Gradle管理的SpringBoot应用程序。到目前为止做了什么:已创建application.yaml、application-dev.yaml和applic... 查看详情

Spring Boot 使用 Yaml 而不是属性文件

】SpringBoot使用Yaml而不是属性文件【英文标题】:SpringBootusingYamlinsteadofpropertiesfile【发布时间】:2017-01-2107:08:06【问题描述】:我使用SpringBoot。我想使用YAML而不是属性来编写我的配置。由于我使用spring-boot-starter,SnakeYAML库已经... 查看详情

Spring Boot MockMVC 测试不加载 Yaml 文件

】SpringBootMockMVC测试不加载Yaml文件【英文标题】:SpringBootMockMVCTestdoesnotloadYamlfile【发布时间】:2015-11-0607:52:49【问题描述】:我的配置位于类路径根目录(src/main/resources/)的application.yml文件中。当我正常启动应用程序时,配置加... 查看详情

Spring Boot:使用 yaml 中配置的 Maven 属性

】SpringBoot:使用yaml中配置的Maven属性【英文标题】:SpringBoot:useofMavenpropertiesconfiguredinyaml【发布时间】:2017-11-0712:31:05【问题描述】:我正在尝试从Maven中提取一些属性并在我的应用程序启动时打印它们。我正在使用SpringBoot,因... 查看详情

基于spring boot读取配置yaml hashmap

】基于springboot读取配置yamlhashmap【英文标题】:Springbootreadingconfigurationyamlhashmapbased【发布时间】:2020-05-3118:54:56【问题描述】:是否可以在SpringBoot中使用@configurationproperties读取yaml文件?app:X1:key1:value1key2:value2X2:key1:value3key2:value4... 查看详情

spring boot 外部配置 - profile 特定配置

】springboot外部配置-profile特定配置【英文标题】:springbootexternalconfiguration-profilespecificconfiguration【发布时间】:2018-12-1316:14:50【问题描述】:根据SpringBoot文档,配置顺序为:打包jar之外的特定于配置文件的应用程序属性(applicat... 查看详情

Spring boot YAML Config未读取所有值

】SpringbootYAMLConfig未读取所有值【英文标题】:SpringbootYAMLConfignotreadingallvalues【发布时间】:2017-06-2800:12:12【问题描述】:我正在尝试在我的SpringBoot1.5.1项目中设置和使用YAML作为配置文件。我的YAML文件如下所示:hue:user:cdKjsOQIRY8... 查看详情

springboot项目中propertysource读取yaml配置文件(代码片段)

  springboot项目中,当我们使用@Value注解读取配置属性,默认的配置文件是properties类型文件,如果一些配置来自yaml格式配置文件,那么就需要做一个配置。PropertySource注解提供了factory属性,可以设置yaml格... 查看详情

将 Yaml 中的列表映射到 Spring Boot 中的对象列表

】将Yaml中的列表映射到SpringBoot中的对象列表【英文标题】:MappinglistinYamltolistofobjectsinSpringBoot【发布时间】:2015-12-1202:52:59【问题描述】:在我的SpringBoot应用程序中,我有application.yaml配置文件,其中包含以下内容。我想将其作... 查看详情

如何在 spring-boot 配置中加载 application.yaml 配置以进行硒测试

】如何在spring-boot配置中加载application.yaml配置以进行硒测试【英文标题】:Howtoloadapplication.yamlconfiginspring-bootconfigurationforseleniumtesting【发布时间】:2014-02-2408:21:24【问题描述】:我正在尝试对我的spring-boot应用程序运行selenium测... 查看详情