explorer
explorer

Reputation: 516

parent properties for application-{profile}.properties with Spring-Boot app

There are common properties which are shared among different profiles for e.g. path location for temp files and path remains same among different env(tst,prd).

Is there a way to have a parent application-{parent}.properties from which all the profile specific properties files can inherit the properties.

That will help in avoiding writing same properties in all application-{profile}.properties

In addition, each application-{profile}.properties have something like :

profileLocation=xxx
abc=${profileLocation}/tempPath

Here can I move abc to a common location? I cannot in application.properties as it gets loaded before application-{profile}.properties

Upvotes: 4

Views: 1806

Answers (1)

Daniel Olszewski
Daniel Olszewski

Reputation: 14401

Actually, that is not entirely true that application.properties are loaded before any others. They are processed together. To set common properties that are used by all profiles, you should use the ordinary application.properties file. Two main thing you should know are described below.

Case 1. The keys that are placed inside the application.properties file can be overridden by profile specific configuration.

common.path.for.all.envs=/some/path
default.path=/another/path

Than in your e.g. application-dev.properties you can override some values.

default.path=/dev/path

At runtime with dev profile your application will have access to two keys. The value of common.path.for.all.envs will be set to /some/path as declared only in the main file and default.path will be set to /dev/path because you override the property in the profile configuration.

Case 2. The values defines in the application.properties file can use placeholders for the values included in profile configurations. For instance, in your application.properties define the following variable:

abc=${profileLocation}/tempPath

Next, in the application-dev.properties declare the missing variable:

profileLocation=xxx

Then running with the dev profile the value of abc will be set to xxx/tempPath. As you see, the variable declared in the profile configuration can be used in the main application.properties file as well.

Upvotes: 2

Related Questions