huzeyfe
huzeyfe

Reputation: 3704

Multiple Spring profiles with different names

I have a Spring Boot application and I have application.yml for properties. I have multiple profiles in the same file like below:

spring:
  profiles: dev
property:
  one: bla
  two: blabla 

---

spring:
  profiles: preProd, prod
another-property:
  fist: bla
  secong: blabla 

---

spring:
  profiles: prod
property:
  one: prod-bla
  two: prod-blabla

So my question is when I run applicaiton with prod profile only does Spring merge both profiles and I can see both property and another-propertyin the app?

Upvotes: 0

Views: 2072

Answers (1)

Dirk Deyne
Dirk Deyne

Reputation: 6936

Merging works perfectly!

given:

@SpringBootApplication
public class SoYamlSpringProfileMergeApplication {

    private final Data data;

    public SoYamlSpringProfileMergeApplication(Data data) {
        this.data = data;
    }

    @EventListener(ApplicationReadyEvent.class)
    public void showData() {
      System.err.println(data.getOne());
      System.err.println(data.getTwo());
      System.err.println(data.getThree());
    }

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

}

@Component
@ConfigurationProperties(prefix = "data")
class Data {

    private String one = "one default";

    private String two = "two default";

    private String three = "three default";

    public String getOne() {
        return one;
    }

    public String getTwo() {
        return two;
    }

    public String getThree() {
        return three;
    }

    public void setOne(String one) {
        this.one = one;
    }

    public void setTwo(String two) {
        this.two = two;
    }

    public void setThree(String three) {
        this.three = three;
    }
}

and

spring:
  profiles:
    active: "other"

---

spring:
  profiles: dev

data:
  one: one dev
  two: two dev

---

spring:
  profiles: prod

data:
  one: one prod
  two: two prod

---

spring:
  profiles: other

data:
  three: three other

will print:

one dev
two dev
three other

and with:

spring:
  profiles:
    active: "other,prod"
one prod
two prod
three other

important the order of active:"other,prod" matters!

using

spring:
  profiles:
    active: "prod,other"

will output

one dev
two dev
three other
  • load props from 'prod'
  • merge with props 'other' merges the dev-values

Upvotes: 2

Related Questions