简体   繁体   English

如何在spring-boot 1.4.3中覆盖spring-boot应用程序属性

[英]How to override spring-boot application properties in spring-boot 1.4.3

In our project we setup our integration tests by overriding the properties in the IntegrationTest annotation as shown below: 在我们的项目中,我们通过覆盖IntegrationTest注释中的属性来设置集成测试,如下所示:

@RunWith(SpringJunitClassRunner.class)
@IntegrationTest("server.port:0",
                  "health.hystrix.enabled:false"
                   .... other properties ....
                )
@ActiveProfile("local","no-swagger")
public class IntegrationTest{
}

However in spring-boot 1.4 @IntegrationTest annotation has been deprecated. 但是在spring-boot 1.4中,不推荐使用@IntegrationTest注释。 Spring documentation suggests using @SpringBootTest annotation instead. Spring文档建议使用@SpringBootTest注释。 My question is How do I override the properties with this new annotation? 我的问题是如何使用这个新注释覆盖属性?

What I understand from the docs is that you are able to override properties inside @SpringBootTest . 从文档中了解到,您可以覆盖@SpringBootTest属性。

The @SpringBootTest annotation also has a properties attribute that can be used to specify any additional properties that should be defined in the Environment. @SpringBootTest注释还具有一个属性属性,可用于指定应在环境中定义的任何其他属性。 Properties are now loaded in the exact same way as Spring's regular @TestPropertySource annotation. 现在,属性的加载方式与Spring的常规@TestPropertySource批注@TestPropertySource

And also the javadoc of @SpringBootTest says : @SpringBootTest的javadoc也说

@AliasFor(value="value")

public abstract String[] properties

Properties in form key=value that should be added to the Spring Environment before the test runs. 表单中的属性 key=value应在测试运行之前添加到Spring环境中的key=value

Returns: the properties to add 返回:要添加的属性

So simply override the properties inside @SpringBootTest annotation. 因此,只需覆盖@SpringBootTest注释中的属性@SpringBootTest

@SpringBootTest(properties={"server.port=0"})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM