简体   繁体   English

如何将 application.properties 与 application-default.properties 合并并包含在 Spring Boot >2.4 的其他测试配置文件中

[英]How to merge application.properties with application-default.properties and include in other test profiles with Spring Boot >2.4

We would like to merge src/main/resources/application.properties with additional default properties when running tests which in turn should be included by other (test) properties files activated via specific profiles in our Spring Boot application.我们希望在运行测试时将src/main/resources/application.properties与其他默认属性合并,而这些默认属性又应包含在通过 Spring 引导应用程序中的特定配置文件激活的其他(测试)属性文件中。

Up until Spring Boot 2.4 this worked quite well by having all common test properties in src/test/resources/application-default.properties to keep things DRY.直到 Spring Boot 2.4 之前,通过在src/test/resources/application-default.properties中包含所有常见的测试属性来保持干燥,这工作得很好。 Those were then automatically merged with the ones from src/main/resources/application.properties by Spring [ 1 ].然后这些被 Spring [ 1 ] 自动与来自src/main/resources/application.properties的那些合并。 This allowed us to have our own set of default properties without requiring tests to specify an @ActiveProfiles("default") .这使我们能够拥有自己的一组默认属性,而无需测试来指定@ActiveProfiles("default")

Other (test) profiles could then have their own application-<profile>.properties with spring.profiles.include=default and then further extend the defaults.然后其他(测试)配置文件可以拥有自己的 application-<profile>.properties 和spring.profiles.include=default ,然后进一步扩展默认值。

With Spring Boot 2.4 I'm struggling with the new "rules":使用 Spring Boot 2.4 我正在努力应对新的“规则”:

  • I can no longer load application-default.properties from application-<profile>.properties since spring.profiles.include is no longer allowed in non profile-specific documents [ 2 ].我无法再从 application-<profile>.properties 加载 application-default.properties,因为spring.profiles.include在非特定于配置文件的文档中不再允许 [ 2 ]。

  • I don't want to introduce a src/test/resources/application.properties since I don't want to repeat everything from src/main/resources/application.properties .我不想介绍src/test/resources/application.properties因为我不想重复src/main/resources/application.properties中的所有内容。 Also I don't want to load any activate any test-related profiles in the app's properties.此外,我不想在应用程序的属性中加载任何激活任何与测试相关的配置文件。

It looks like one solution could be to explicitly add spring.profiles.include=default to src/main/resources/application.properties to force the application to include the properties file with default properties which will work as before when it comes to running the actual application but consider src/test/resources/application-default.properties when running the tests.看起来一种解决方案可能是将spring.profiles.include=default显式添加到src/main/resources/application.properties以强制应用程序包含具有默认属性的属性文件,这在运行实际应用程序,但在运行测试时考虑src/test/resources/application-default.properties

Is this the way to go or are there smarter solutions to tackle this problem and still keep the properties free of redundancies?这是通往 go 的方法,还是有更聪明的解决方案来解决这个问题,并且仍然保持属性没有冗余?

spring.profiles.active=dev spring.profiles.active=dev

add this config in application.properties file在 application.properties 文件中添加此配置

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用application.properties文件激活配置文件以进行弹簧启动测试 - Activate profiles for spring boot test using application.properties file 如何排除默认的application.properties在Spring启动项目的Maven中使用配置文件添加自定义属性文件? - How to exclude default application.properties add custom properties file using profiles in maven for spring boot project? 如果配置文件设置不更改为 application-default.properties,如何忽略 application.properties 值 - How to ignore application.properties values if profile set without changing to application-default.properties 作为WAR在Springcat上部署的Spring Boot,如何针对不同配置文件(Dev,Test,Staging,Prod)外部化application.properties - Spring boot deployed as WAR on tomcat, How to Externalize application.properties for different profiles(Dev,Test,Staging,Prod) 如何在Spring Boot集成测试中覆盖application.properties? - How to override application.properties in Spring Boot integration test? Spring Boot - 配置文件(application.properties)为 HTTP 请求定义 baseUrl - Spring Boot - Profiles (application.properties) to define baseUrl for HTTP requests Spring 引导中的配置文件特定 application.properties 文件和多个配置文件的默认值注入 - Profile specific application.properties file in Spring boot and default injection of values for multiple profiles 如何阅读Spring Boot application.properties? - How to read Spring Boot application.properties? 用application.properties覆盖默认的Spring-Boot application.properties - Override default Spring-Boot application.properties with application.properties 合并许多application.properties文件而不是Spring Boot上的替换? - merge many application.properties files instead of replace on Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM