0

I have multiple profiles defined in my Spring boot application, Typically for different scenarios.

At startup, I specify the profiles to be applied and they are activated.

At run-time, I am looking for a way to activate profiles, without restarting the app.

I know Spring cloud config provides a way to externalize configuration and reload with restarting the app using actuator /refresh endpoint.

I changed the property spring.profiles.active=profileName in externalized configuration maintained by Spring Cloud Config for the application, to a different profile value and then reloaded using /refresh endpoint. But, changes are not getting reflected. I used native profile of Spring cloud config.

But, I need to change the profile or add profiles after the app is started using Spring cloud Config/actuator or some other mechanism.

Is there a way to accomplish my requirement.

juser
  • 359
  • 1
  • 7
  • 17
  • 1
    `/refresh` will only affect beans tagged with a `@RefreshScope`. http://projects.spring.io/spring-cloud/spring-cloud.html#_refresh_scope – Darren Forsythe Jun 28 '17 at 12:39

1 Answers1

0

Profiles are used to decide which beans created at startup. So if want to run under a different set of profiles you have to restart.

What are you actually trying to achieve ?

If you want runtime selection of beans then write a class that injects all the available beans you want to pick from and then select which one you want to use at runtime via a method that selects based on an environment property that you can refresh via Spring Cloud Config/refresh endpoint, or just via a database column.

Update - the requirement is to disable caching at runtime. This can be done as follows:

Write a method that determines whether or not a particular profile is active (environment is your injected Environment)

boolean isProfileActive(String profile) { 
   return Arrays.asList(environment.getActiveProfiles()).contains(profile);
}

then use that for your spel condition on the cacheable annotation

PaulNUK
  • 4,774
  • 2
  • 30
  • 58
  • Thanks for your response. My requirement is to enable/disable caching in the service methods. I currently have "@Cacheable" annotation applied to each of my Service Methods and I use a custom configuration with the "@EnableCaching" annotation as a custom profile which I activate on startup. Now, I want to enable/disable the caching after startup. – juser Jun 28 '17 at 13:43
  • Ah OK, see my answer here for disabling caching at runtime based on an active profile. https://stackoverflow.com/questions/35917159/spring-boot-how-to-disable-cachable-during-development I'll update my answer too. – PaulNUK Jun 29 '17 at 09:16