0
votes

Env Details:

  • Spring Boot 2.1.15.RELEASE
  • Spring 5.2.2.RELEASE

I have application.yml file as below,

creds:
    first: default value
    
---

spring.profiles: dev

creds:
    first: dev value


---

spring.profiles: dev & mobile

creds:
    first: dev-mobile value

and

spring.profiles.active=dev,mobile

However, the problem is that it always loads 'default value'.

Property under profile 'dev & mobile' is never loaded.

Appreciate any help.

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-multi-profile-yaml

2
You are using yml format, its not spring.profiles, its spring: profiles:Dinesh Dontha
@DineshDontha - tried that too, but didn't work.Vijay

2 Answers

0
votes

Firstly, there is no way that your application.properties will pick the combined profile (profile: dev & mobile) if you define profiles individually(profile: dev) & also combining it with other profile again(combined with mobile again, I mean dev & mobile). This does not give you any Exceptions, but your combined profile will not get a pick.

The documentation says that, we can combine profiles with symbols & | and other symbols please refer docs, we can make such any complex combinations. But, I don't recommend you define a profile individually & also combine it with other profiles, like you combined dev & mobile, after define dev profile separately.

Following example shows, how it works?

application.yaml

creds:
  first: default value

---

spring:
  profiles: dev

creds:
  first: dev value

---

spring:
  profiles: mobile

creds:
  first: mobile val

---

spring:
  profiles: dev & mobile

creds:
  first: dev-mobile value

What happens, if we define a profile individually & combining it with other profile? (like you did).

In this application.yml file, I have defined dev, mobile & combined profile(dev & mobile), and as we know there is also a default profile.

If I don't invoke any profile, as we know, it will pick default profile. For Example, if we are reading a property like this:

@Value("${creds.first}")
String value;

property value: default value

If --spring.profiles.active=dev , then it will pick dev profile.

property value: dev value

If --spring.profiles.active=mobile , then it will pick mobile profile.

property value: mobile val

If --spring.profiles.active=mobile,dev , then here, we have trying to invoke both profiles at same time, ideally, we expect the combined profile should be picked. But it will be not picked. Because, we have defined separate profiles(profile: dev and profile: mobile separately), firstly, it will read the property from mobile profile and then it reads the property from dev profile, if found, then it overrides with property value from dev profile. If not found, it will stick to property value of mobile profile only. I mean to say, it reads

property value: dev value

If --spring.profiles.active=dev,mobile, then here, it first reads the property value from dev profile, then, the again it looks for the property in mobile profile, if found, overrides the value. If not found, it will stick to property value of dev profile only.

That's why it will not read the combined profile (dev & mobile).

How & when it reads combined profile?

As per documentation, it can pick a combined profile, if the mentioned profiles should not have been defined separately elsewhere:

Example: application.yaml

creds:
  first: default value

---

spring:
  profiles: showcase

creds:
  first: showcase value

---

spring:
  profiles: dev & mobile

creds:
  first: dev-mobile value

here, dev, mobile are combined using & and they are not separately defined elsewhere. So, now if we invoke --spring.profiles.active=dev,mobile, it will pick the combined profile value

property value: dev-mobile value

Lastly, if --spring.profiles.active=dev or --spring.profiles.active=mobile, it will not pick the combined profile. So, it will sick to default profile.

property value: default value

Sample: https://github.com/donthadineshkumar/multi-profile-document.git

0
votes

Seems you added every environments in same file it should be one file for dev application-dev.yml

reds:
    first: dev value

another file for production

application-production.yml

reds:
    first: production value

goes on.