fortm
fortm

Reputation: 4208

adding new property based on spring profile

If spring boot is run in override profile , can we have application-override.properties having properties like foo.baz that is not defined in application.properties ?

application.properties

foo.bar=1

application-override.properties

spring.profiles.include=default 
foo.baz=1

Upvotes: 2

Views: 3284

Answers (4)

davidxxx
davidxxx

Reputation: 131496

Long story short : Spring boot overrides values of properties with same name according to their evaluation order. But here you don't override any property, you add a new.

That is still simpler : Spring boot just adds it into the Spring Environment.
Just run the app by specifying this profile and makes sure that the properties are located in the locations expected by Spring Boot.

Example from a fat jar (Java system property) :

java -Dspring.profiles.active=override -jar foo.jar

Example from the source code (Maven property) :

mvn spring-boot:run -Dspring-boot.run.profiles=override

Upvotes: 2

Zack
Zack

Reputation: 4047

Yes, you can do this by simply add the profile name to the application.properties:

application-override.properties

Then you can load profile from the command line:

java -jar foo.jar --spring.profiles=override

source: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-change-configuration-depending-on-the-environment

Spring will load the application.properties first followed by any application-{profile}.properties.


Another option is to use yaml, and load everything into one file:

foo:
  bar: 1

---

spring:
  profiles: override
foo:
  baz: 1

---

spring:
  profiles: otherOverride
foo:
  bar: 2
  baz: 2

Upvotes: 1

Nazeem
Nazeem

Reputation: 764

That is correct. When you have new properties in application-override.properties and and the override profile is the active profile, then yes in your program the properties from application.properties as wel as application-override.properties is loaded.

Using spring.profiles.include=default in your override profile is not needed.

In the case of loading multiple specific profiles with same properties:

Also, in the context of property overriding with profiles, something to keep in mind when you have multiple active profiles and they contain the same property. The last profile in the list will be used.

Let's say you start up your program with mvn spring-boot:run -Drun.profiles=profile1,profile2

Both application-profile1.properties and application-profile2.properties contains the property my.custom-property=x (for profile1) and my.custom-property=y (for profile2). The value of my.custom-property will be y, as that was the last profile in the provided profiles.

Upvotes: 3

Jakubeeee
Jakubeeee

Reputation: 696

You can create configuration class for your custom profile and load the appropriate properties file in it like this:

@Configuration
@Profile("override")
@PropertySource("classpath:application-override.properties")
public class OverrideConfig {

}

This way, all the configuration you do in OverrideConfig (including taking properties from application-override.properties), will only load if override profile is enabled in application.properties like this:

spring.profiles.active=override

Upvotes: 2

Related Questions