gpt4 book ai didi

spring - 如何使用 Spring 配置文件表达式?

转载 作者:行者123 更新时间:2023-12-03 03:45:18 26 4
gpt4 key购买 nike

Spring Boot:2.0.4.RELEASE

我指定了 Spring Boot multi-profile YAML配置文件:

server:
address: 192.168.1.100
---
spring:
profiles: development
server:
address: 127.0.0.1
---
spring:
profiles: production | eu-central
server:
address: 192.168.1.120

根据reference guide ,如果 生产 eu-central 配置文件处于事件状态,则 server.address 属性为 192.168 .1.120。但是当我运行这个测试时

@ActiveProfiles({"production"})
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProfileTest {

@Autowired
private Environment environment;
@Value("${server.address}")
private String serverAddress;

@Test
public void testProfile() {
assertThat(environment.getActiveProfiles(), is(new String[] {"production"} ));
assertThat(serverAddress, is("192.168.1.120"));
}

}

失败了:

java.lang.AssertionError: 

Expected: is "192.168.1.120"
but: was "192.168.1.100"
at com.example.demo.ProfileTest.testProfile(ProfileTest.java:27)

为什么测试失败以及如何正确使用 Spring 配置文件表达式?顺便说一句,如果我删除 |来自 spring.profiles 键的 eu-central 测试通过!

最佳答案

鲍里斯。我研究过你的问题。我认为我在 Spring-boot 中发现了一个错误。我已经报告了一个问题,您可以检查一下。问题是 SpringBoot 总是使用 ${server.address} 的默认值,因为框架无法识别配置文件:生产 | eu-central 作为配置文件列表。如果我使用

@ActiveProfiles(value = "production | eu-central")

一切都好。

关于 Spring-boot 问题的链接在这里 https://github.com/spring-projects/spring-boot/issues/14314

所以,你可以看到讨论。谢谢你!

已更新

我已经从 Spring 团队得到了关于我的问题的答复。

This format is a Spring Framework 5.1 (and therefore a Spring Boot 2.1 feature).

所以你的问题的答案是此功能将在 Spring Boot 2.1 中提供。

关于spring - 如何使用 Spring 配置文件表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52183168/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com