I've set the environment variable SPRING_PROFILES_ACTIVE
on my local linux machine:
$ echo $SPRING_PROFILES_ACTIVE
development,develop,devel,dev
In my servlet initializer I'm setting a default profile as I didn't set any variables on my productive machine by extending AbstractDispatcherServletInitializer
and overriding createRootApplicationContext
:
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
StringBuilder sb = new StringBuilder();
for (String s : rootContext.getEnvironment().getActiveProfiles()) {
sb.append(s).append(",");
}
log.debug("Active Profiles: " + sb.toString());
sb = new StringBuilder();
for (String s : rootContext.getEnvironment().getDefaultProfiles()) {
sb.append(s).append(",");
}
log.debug("Default Profiles: " + sb.toString());
rootContext.getEnvironment().setDefaultProfiles("production");
sb = new StringBuilder();
for (String s : rootContext.getEnvironment().getActiveProfiles()) {
sb.append(s).append(",");
}
log.debug("Active Profiles: " + sb.toString());
sb = new StringBuilder();
for (String s : rootContext.getEnvironment().getDefaultProfiles()) {
sb.append(s).append(",");
}
log.debug("Default Profiles: " + sb.toString());
return rootContext;
That's the output:
DEBUG c.e.WebApplicationInitializer - Active Profiles:
DEBUG c.e.WebApplicationInitializer - Default Profiles: default,
DEBUG c.e.WebApplicationInitializer - Active Profiles:
DEBUG c.e.WebApplicationInitializer - Default Profiles: production,
Why there are no active profiles?
UPDATE
Log output with level TRACE:
DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [servletConfigInitParams] PropertySource with lowest search precedence
DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [servletContextInitParams] PropertySource with lowest search precedence
DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [jndiProperties] PropertySource with lowest search precedence
DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
DEBUG o.s.w.c.s.StandardServletEnvironment - Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
TRACE o.s.c.e.PropertySourcesPropertyResolver - getProperty("spring.profiles.active", String)
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [servletConfigInitParams]
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [servletContextInitParams]
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [jndiProperties]
DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/spring.profiles.active]
DEBUG o.s.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/spring.profiles.active] not found - trying original name [spring.profiles.active]. javax.naming.NameNotFoundException: Name [spring.profiles.active] is not bound in this Context. Unable to find [spring.profiles.active].
DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [spring.profiles.active]
DEBUG o.s.jndi.JndiPropertySource - JNDI lookup for name [spring.profiles.active] threw NamingException with message: Name [spring.profiles.active] is not bound in this Context. Unable to find [spring.profiles.active].. Returning null.
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [systemProperties]
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [systemEnvironment]
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'spring.profiles.active' in any property source. Returning [null]
DEBUG c.e.WebApplicationInitializer - Active Profiles:
TRACE o.s.c.e.PropertySourcesPropertyResolver - getProperty("spring.profiles.default", String)
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.default' in [servletConfigInitParams]
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.default' in [servletContextInitParams]
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.default' in [jndiProperties]
DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/spring.profiles.default]
DEBUG o.s.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/spring.profiles.default] not found - trying original name [spring.profiles.default]. javax.naming.NameNotFoundException: Name [spring.profiles.default] is not bound in this Context. Unable to find [spring.profiles.default].
DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [spring.profiles.default]
DEBUG o.s.jndi.JndiPropertySource - JNDI lookup for name [spring.profiles.default] threw NamingException with message: Name [spring.profiles.default] is not bound in this Context. Unable to find [spring.profiles.default].. Returning null.
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.default' in [systemProperties]
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.default' in [systemEnvironment]
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'spring.profiles.default' in any property source. Returning [null]
DEBUG c.e.WebApplicationInitializer - Default Profiles: default,
TRACE o.s.c.e.PropertySourcesPropertyResolver - getProperty("spring.profiles.active", String)
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [servletConfigInitParams]
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [servletContextInitParams]
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [jndiProperties]
DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/spring.profiles.active]
DEBUG o.s.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/spring.profiles.active] not found - trying original name [spring.profiles.active]. javax.naming.NameNotFoundException: Name [spring.profiles.active] is not bound in this Context. Unable to find [spring.profiles.active].
DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [spring.profiles.active]
DEBUG o.s.jndi.JndiPropertySource - JNDI lookup for name [spring.profiles.active] threw NamingException with message: Name [spring.profiles.active] is not bound in this Context. Unable to find [spring.profiles.active].. Returning null.
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [systemProperties]
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [systemEnvironment]
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'spring.profiles.active' in any property source. Returning [null]
DEBUG c.e.WebApplicationInitializer - Active Profiles:
DEBUG c.e.WebApplicationInitializer - Default Profiles: production,
UPDATE 2
I've exported the environment variable by creating a file in /etc/profile.d
:
DEBUG c.e.WebApplicationInitializer - System.getenv("SPRING_PROFILES_ACTIVE"): null