I'm having problems overriding the default application properties by specifying the active profile by spring.profiles.active
property.
I have developed a sample project to replicate this issue. I have 2 application.yml files:
application-default.yml
spring:
profiles:
active: local
test:
value: "This is the test value for application properties of default"
application-local.yml
test:
value: "This is the test value for application properties of local"
I have then developed a component to print the test value:
@Component
public class TestComponent {
@Value("${test.value}")
private String testValue;
@PostConstruct
public void postConstruct() {
System.out.println(testValue);
}
}
The output of this code is:
2021-01-07 16:19:01.080 INFO 335505 --- [ main] com.test.Application : The following profiles are active: local
This is the test value for application properties of default
2021-01-07 16:19:01.419 INFO 335505 --- [ main] com.test.Application : Started Application in 0.944 seconds (JVM running for 1.441)
I expected the output would be:
This is the test value for application properties of local
I'm aware that using application.yml
instead of application-default.yml
works as intended, but I was aiming to use application.yml
to hold base properties common to all profiles and specify environment-specific configurations through the use of profiles.
I'm using spring-boot 2.4.0, build.gradle
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
implementation group: 'org.springframework.boot', name: 'spring-boot-starter', version: '2.4.0'
}
Why doesn't this property gets overridden?