How to use a Spring profile expression?

Viewed 2334

Spring Boot: 2.0.4.RELEASE

I have specified a Spring Boot multi-profile YAML configuration file:

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

According to the reference guide, if the production or eu-central profile is active, the server.address property is 192.168.1.120. But when I run this test

@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"));
  }

}

it fails:

java.lang.AssertionError: 

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

Why does the test fail and how do I use a Spring profile expression correctly? By the way, if I remove | eu-central from the spring.profiles key the test passes!

1 Answers

Boris. I've studied your problem. And I think that I've found a bug in Spring-boot. I've reported an issue so you can check it. The problem is SpringBoot always use default value for ${server.address} because framework couldn't recognise profiles: production | eu-central as a List of profiles. If I use

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

All is ok.

The link on Spring-boot issue here https://github.com/spring-projects/spring-boot/issues/14314

So, you could see the discussion. Thank you!

UPDATED

I've got an answer from Spring Team on my Issue.

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

So the answer on your question is This feature will be available in Spring Boot 2.1.

Related