简体   繁体   English

如何在springboot应用程序中从yaml文件读取属性值?

[英]How to read the property value from yaml file in springboot application?

In my project I have a yaml file like this : service: url: http://10.10.100.09/service/ping 在我的项目中,我有一个yaml文件,如下所示:service:url: http : //10.10.100.09/service/ping

Currently I have setup my tests like this: 目前,我已经按照以下步骤设置了我的测试:

@RunWith(SpringRunner.class)
@SpringBootTest(properties = 
{"service.url=http://10.10.100.09/service/ping"},webEnvironment = 
SpringBootTest.WebEnvironment.RANDOM_PORT)
public abstract class BaseIntegration {

@Value("${local.server.port}")
private int serverPort;

@Before
public void setup() {
    RestAssured.port = serverPort;
}

} }

My test are extending the BaseIntegration class and the test code reside there. 我的测试正在扩展BaseIntegration类,并且测试代码驻留在该类中。

Question: How can I read the url property directly from the config file and not hardcord it in the tests? 问题:如何直接从配置文件中读取url属性,而不在测试中进行硬编码?

I did try using the @SpringBootApplication annotation but its giving error when I start my tests. 我确实尝试使用@SpringBootApplication批注,但在开始测试时却给出了错误。

can you try 你能试一下吗

@SpringBootTest(properties = {"${service.url}"},webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

or 要么

@SpringBootTest(properties = {"service.url"},webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

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

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