7

Is it possible to have a profile in spring boot and also another profile that inherits most of the parent values and beans?

For example I have two profiles staging and staging-task. I want staging-task to inherit the database configuration of the staging profile however I want it to override the jpa configuration.

Is profile inheritance available to @Configuration beans.

gkatzioura
  • 2,655
  • 2
  • 26
  • 39

2 Answers2

9

Yes. Its possible to have spring profiles in spring boot. For your problem, put your common database configuration in application.yml(default profile).

And you can override the other properties in application-stage.yml.

Spring will read the properties from application.yml and override from application-stage.yml when active profile is stage.

Jaydeep Rajput
  • 3,605
  • 17
  • 35
  • I see. This is a good feature. Is profile inheritance available to @Configuration beans? Thank you! – gkatzioura Apr 03 '17 at 18:52
  • 2
    What about more than one level of inheritance? e.g. foo-dev, foo-prod inheriting from foo and bar-dev, bar-prod inheriting from bar – Jon Freedman Jun 23 '17 at 08:42
0

TL;DR

The active profiles will cause respective application-$profile.properties to be read (if they exist) in the order the active profiles are defined. Later read properties override earlier ones. That will give you the means to do smth. like an hierarchy.

Long Version

There is no such thing as profile-inheritance in spring but it can be mimicked as written in the answer by JRR.

For how it actually works read: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties.

elonderin
  • 509
  • 4
  • 12