Несколько пружинных профилей с одинаковым именем - PullRequest
0 голосов
/ 30 мая 2019

У меня есть приложение Spring Boot, и у меня есть application.yml для свойств.У меня есть несколько профилей в одном файле, как показано ниже:

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

Поэтому мой вопрос: когда я запускаю приложение с prod profile only , Spring объединяет оба профиля, и я вижу обаproperty и another-property в приложении?

1 Ответ

2 голосов
/ 30 мая 2019

Слияние работает отлично!

Дано:

@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;
    }
}

и

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

напечатает:

one dev
two dev
three other

и с:

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

важно порядок активен: "прочее, прод" имеет значение!

с использованием

spring:
  profiles:
    active: "prod,other"

выдаст

one dev
two dev
three other
  • загрузить реквизит из 'prod'
  • объединить с реквизитами 'other' объединить значения dev
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...