[英]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 的方法,还是有更聪明的解决方案来解决这个问题,并且仍然保持属性没有冗余?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.