5

I would like to set the Profile using application.properties file with the entry:

mode=master

How to set spring.profiles.active in my context.xml file? init-param works only in a web.xml context.

<init-param> 
    <param-name>spring.profiles.active</param-name>
    <param-value>"${mode}"</param-value>
</init-param>
luksmir
  • 3,104
  • 5
  • 22
  • 38

3 Answers3

8

There are a few ways to change active profiles, none of which take directly from a properties file.

  • You can use the <init-param> as you are doing in your question.
  • You can provide a system parameter at application startup -Dspring.profiles.active="master"
  • You can get the ConfigurableEnvironment from your ApplicationContext and setActiveProfiles(String...) programmatically with context.getEnvironment().setActiveProfiles("container");

You can use an ApplicationListener to listen to context initialization. Explanations on how to do that here. You can use a ContextStartedEvent

ContextStartedEvent event = ...; // from method argument
ConfigurableEnvironment env = (ConfigurableEnvironment) event.getApplicationContext().getEnvironment();
env.setActiveProfiles("master");

You can get the value "master" from a properties file as you see fit.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • The problem is that is used in Spring MVC which I am not using. What is equivalent to in Spring core ? – luksmir Sep 05 '13 at 05:23
  • 2
    @luksmir There is no equivalent. If you control the context creation, just straight up use the third method and `setActiveProfiles()`. – Sotirios Delimanolis Sep 05 '13 at 12:29
  • 1
    Thanks for the explanation there is no alternative to setActiveProfiles() in my case. – luksmir Sep 05 '13 at 12:37
  • 1
    Unfortunately, `ContextStartedEvent` is raised after the application context initialized. – fwonce Jan 15 '16 at 03:56
  • 1
    @SotiriosDelimanolis Shouldn't last part of the answer be removed or edited at least, as the event is raised after context initialisation, and thus setting the profile has no impact whatsoever? Or else people are going to try this and fail miserably. – Sumit Jain Jul 17 '18 at 05:45
3

You can use either a environment variable, system variable (-D option for the JVM or application) or put it in JNDI (java:comp/env/. You cannot however put it in a properties file, as it is needed before the that specific properties file is read.

There is more information in the @Profile javadocs.

Another solution is to create your own ApplicationContextInitializer implementation which reads a certain file and activates the given profile.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
0

You also can achieve this indirectly via System.setProperty:

// spring.profiles file: profile1,profile2
String anotherProfiles = Files.readString(Path.of("spring.profiles")); // or any other file
// Even some logic can be applied here to anotherProfiles
System.setProperty("spring.profiles.include", "dev," + anotherProfiles)

This sample can be rewritten a bit to read your application.properties file and take specified profiles for Spring.

cybersoft
  • 1,453
  • 13
  • 31