1

I try to set active profile as follows:

Application.properties (inside classpath)

spring.profile.active = ${app.profile}

The property "app.profile" comes from Application.properties outside JAR:

Application.properties file (external)

app.profile = dev

Spring refusing to load the "dev" profile. when i set the "spring.profile.active" property inside classpath it work as expected.

There is an alternative?

Thanks

Idan
  • 201
  • 3
  • 5

2 Answers2

3

you can specify the profile name while starting the Spring boot app.

running from CMD

java -jar my-first-spring-boot-app.jar -Dspring.profiles.active=dev

Running from Eclipse
add spring.profiles.active=dev as VM argument

Refer the following url if you want to pro-grammatically set profile.
Spring Boot Programmatically setting profiles

Jobin
  • 5,610
  • 5
  • 38
  • 53
  • Is it possible to change the property name? cause user will edit these external properties – Idan Dec 06 '16 at 11:47
  • no property cant change (spring.profiles.active) only the value(dev) is changable – Jobin Dec 06 '16 at 11:48
  • My goal is that the user will change the "app.profile" property and then spring.profile.active = ${app.profile} will set the right profile. – Idan Dec 06 '16 at 11:54
  • all other external properties work as expected, except the profile – Idan Dec 06 '16 at 11:55
0

You can set the active profile in web.xml file of your web application. Please see below code snippet:

<context-param>
  <param-name>spring.profiles.active</param-name>
  <param-value>dev, testdb</param-value>
</context-param>

If their is no web.xml file and you are configuriong your web application using Java, then you can use WebApplicationInitializer. Please see below code snippet:

class WebAppInitializer extends WebApplicationInitializer {

  void onStartup(ServletContext container) {
      AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
      rootContext.getEnvironment().setActiveProfiles("profileName");
      rootContext.register(SpringConfiguration.class);
      container.addListener(new ContextLoaderListener(rootContext));
  }
}

WebAppInitializer need to be annotated with @Configuration for this to work.

Please let me know if it helps or you need more help.

SachinSarawgi
  • 2,632
  • 20
  • 28
  • i am working on app with REST only. "WebApplicationInitializer" is the right way? – Idan Dec 06 '16 at 11:41
  • My goal is that the user will change the "app.profile" property and then spring.profile.active = ${app.profile} will set the right profile. all other external properties work as expected, except the profile. – Idan Dec 06 '16 at 12:04