Profile activation based on available environment variables or any custom condition

733 views Asked by At

I was reading this article from Spring Boot Blog and following section attracted attention.

Cloud Platform Activation If you only want volume mounted config trees (or any properties for that matter) to be active on a specific cloud platform, you can use the spring.config.activate.on-cloud-platform property. This works in a similar way to the spring.config.activate.on-profile property, but uses Cloud Platform values, rather than profile names.

If we want to only enable our configtree example above when we’re deployed to Kubernetes, we can do the following:

>    ```spring.config.activate.on-cloud-platform=kubernetes```
>    ```spring.config.import=configtree:/etc/config```

Its useful.

I am looking for a way to activate profile based on some condition for example an environment variable based on cloud region, or any other condition like environment variables.

If I set active profile using following:

spring.profiles.active = profile1, profile2, profile3

Based on deployed environment I would like to add conditionlaProfile-na-east, conditionlaProfile-au-west and configure some resources that local to that region.

Expected behavior Spring Boot should have equivalent to is (during runtime, It would not be possible to change the active profile property.):

spring.profiles.active = profile1, profile2, profile3, conditionlaProfile-na-east

conditionlaProfile-na-east profile overrides any property in profile1, profile2, profile3 before spring context is initialized and for example database related beans are created.

Is it possible by using any existing simple way or by using EnvironmentPostProcessor?

I tried using EnvironmentPostProcessor and calling (ConfigurableEnvironment) environment.addActiveProfiles("profile_name"), but profile gets added to start of list and also environment.getActiveProfiles() returns empty array during first few invocations so can not really use environment.setActiveProfiles(PROFILE-NAME-ARRAY).

Is there any right object exposed by Spring Boot that does the job in one go?

0

There are 0 answers