Property resolving for multiple Spring profiles (yaml configuration)

ε祈祈猫儿з 提交于 2020-03-04 23:07:10

问题


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

来源:https://stackoverflow.com/questions/60333056/property-resolving-for-multiple-spring-profiles-yaml-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!