Sudhanshu Saini
Sudhanshu Saini

Reputation: 177

Spring profile properties inheritance with single properties file

I have one application.yml file containing multiple spring profiles. I want to inherit properties from one profile to another.

In this example I want to inherit properties of prod profile into prod1 without writing common properties again in prod1 profile.

  server:
    port: 8080

  spring:
    datasource:
      driver-class-name: com.mysql.cj.jdbc.Driver
    application:
      name: TestApp

    URL: "https://localhost:8181/Services/IDEA-Client-Partners"
  ---
  spring:
    profiles: dev
  ---
  spring:
    profiles: prod

  URL: https://www.ideaedu.org/Services/IDEA-Client-Partners

  ---
  spring:
    profiles: prod1

Upvotes: 0

Views: 1408

Answers (1)

Darren Forsythe
Darren Forsythe

Reputation: 11411

properties do already inherit if multiple profiles are activate. E.g. if you activate prod, and prod1 all default < prod < prod1 properties will activate with default, getting overwritten by anything in prod, and prod getting overwritten by anything in prod1.

Given your example,

 server:
    port: 8080

  spring:
    datasource:
      driver-class-name: com.mysql.cj.jdbc.Driver
    application:
      name: TestApp

    URL: "https://localhost:8181/Services/IDEA-Client-Partners"
  ---
  spring:
    profiles: dev
  ---
  spring:
    profiles: prod

  URL: https://www.ideaedu.org/Services/IDEA-Client-Partners
  prodProperty: test
  ---
  spring:
    profiles: prod1

  URL: https://localhost/

And activating all profiles, -Dspring.profiles.active=prod,prod1

the following properties will be set,

  • port = 8080
  • diver-class-name= com.mysql.cj.jdbc.Driver
  • name = TestApp
  • prodProperty = test
  • URL = https://localhost/

In case of conflicting properties e.g. URL in this example, the last property read wins i.e. when prod and prod1 are active the last property read will in, prod1's definition in this case.

Upvotes: 1

Related Questions