1

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?

2 Answers2

1

This is caused due to the order that spring boot is loading the properties. Create an application.yml file with the active profile, and the common properties for all profiles and remove the spring.profiles.active property from the application-default.yml .

spring:
  profiles:
    active: local

Also you could select the active profile in runtime like this

java -jar example.jar --spring.profiles.active=local
  • Passing the active profile by command line works as intended, but I looking to solve it without that in order of better developer comfort. Yes, I'm aware that `application.yml` works as intended, although that is not what I'm looking for. I was aiming to use `application.yml` as a base properties file for every profile and use the properties of profiles to specify environment-specific configurations. – Ricardo Correia Jan 08 '21 at 09:24
0

The default property file should be called application.yml and not application-default.yml

Just try renaming that and it should work fine.. read more here

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-files-profile-specific

j.i.t.h.e.s.h
  • 4,128
  • 2
  • 15
  • 7
  • 1
    Yes, I'm aware that `application.yml` works as intended, although that is not what I'm looking for. I was aiming to use `application.yml` as a base properties file for every profile and use the properties of profiles to specify environment-specific configurations. – Ricardo Correia Jan 08 '21 at 09:22
  • that is exactly how it works. It sets all the properties from application.yml for any profile you pass, but if you define anything in application-profile.properties and use that profile it overrides them with what you define in that. – j.i.t.h.e.s.h Jan 08 '21 at 21:17
  • So in my application I have application.properties(not yml) which has all the common configuration e.g. logging levels are set to INFO and then application-dev.properties where i set the logging levels to TRACE, application-uat.properties where i set the logging levels to DEBUG. While running my spring boot application I pass -Dspring.profiles.active=dev as command line argument. – j.i.t.h.e.s.h Jan 08 '21 at 21:20