Betlista
Betlista

Reputation: 10549

Property resolving for multiple Spring profiles (yaml configuration)

Is there a defined order when multiple profiles are used for a property resolution.

I have yaml configuration file:

name: none
---
spring:
  profiles: prod
name: prodName
---
spring:
  profiles: dev
name: devName

When application runs with no (default) profile, none is printed. For dev/prod profiles devName/prodName is printed (so far so good).

When I tried to define profile as dev,prod prodName is printed,when I specify prod,dev devName is printed.

Is this something I can rely on? I mean is it specified in Spring? I didn't find it here.

full version (for replication)

application.yml

name: none
---
spring:
  profiles: dev
name: devName
---
spring:
  profiles: prod
name: prodName

Configuration.java

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@ConfigurationProperties
public class Configuration {

    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

SpringBootConsoleApplication.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.List;

@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {

    private static Logger LOG = LoggerFactory.getLogger(SpringBootConsoleApplication.class);

    @Autowired
    Configuration conf;

    public static void main(String[] args) {
        SpringApplication.run(SpringBootConsoleApplication.class, args);
    }

    @Override
    public void run(String... args) {
        LOG.info("name: {}", conf.name);
    }

}

edit:

GitHub repository

sample output:

c:\betlista\SpringPropertyResolutionMultipleProfiles>java -jar target\spring-boot-console-app-1.0.jar
...: name: none, label: labelValue

c:\betlista\SpringPropertyResolutionMultipleProfiles>java -jar -Dspring.profiles.active=dev target\spring-boot-console-app-1.0.jar

...: name: devName, label: labelValue

c:\betlista\SpringPropertyResolutionMultipleProfiles>java -jar -Dspring.profiles.active=dev,prod target\spring-boot-console-app-1.0.jar

...: name: prodName, label: labelValue

c:\betlista\SpringPropertyResolutionMultipleProfiles>java -jar -Dspring.profiles.active=prod,dev target\spring-boot-console-app-1.0.jar

...: name: devName, label: labelValue

edit 2:

Topics

There was a question which I'd refer to as topics - Multiple properties file for a single spring profile

While one can use @PropertySource with property files, it cannot be used with YAML files. The only solution I know at the moment is to used e.g. -Dspring.config.additional-location=classpath:topic1.yml

edit 3:

The "duplicate" question has same questions as I have here, but without an answer. That question was about beans, not really a properties. Accepted answer says "spring.profiles.active system property doesn't matter.", which I shown as incorrect (within context of properties). Please vote for reopen if you agree.

Upvotes: 2

Views: 8345

Answers (1)

Stephane Nicoll
Stephane Nicoll

Reputation: 33091

Is this something I can rely on?

Yes, the last-win strategy is consistent and is documented in the reference guide:

If several profiles are specified, a last-wins strategy applies. For example, if profiles prod,live are specified by the spring.profiles.active property, values in application-prod.properties can be overridden by those in application-live.properties.

(Unrelated, but about the link to the doc you've shared. I don't know if that's an accident but that's the documentation for Spring Boot 1.2.x which was released over 6 years ago).

Upvotes: 2

Related Questions