Profile Inheritage In spring boot
Asked Answered
B

2

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.

Beadruby answered 3/4, 2017 at 18:23 Comment(0)
M
8

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.

Mayence answered 3/4, 2017 at 18:47 Comment(2)
I see. This is a good feature. Is profile inheritance available to @Configuration beans? Thank you!Beadruby
What about more than one level of inheritance? e.g. foo-dev, foo-prod inheriting from foo and bar-dev, bar-prod inheriting from barBossuet
C
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.

Chellean answered 6/3, 2019 at 12:54 Comment(2)
What's LT;DR???Jiggle
@AdrianM. a typo. Fixed it.Chellean

© 2022 - 2024 — McMap. All rights reserved.