Reputation: 379
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.
Upvotes: 0
Views: 2847
Reputation: 5209
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
Upvotes: 0