4

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
dtrunk
  • 4,685
  • 17
  • 65
  • 109
  • Does the debug output from Spring mention where it is looking to locate the active profile? If not, does the TRACE level give any more info? – geoand Oct 14 '14 at 17:00
  • It is mentioned here: http://gordondickens.com/wordpress/2012/06/12/spring-3-1-environment-profiles/ (This token can be set as: an Environment Variable, a JVM Property, Web Parameter, Programmatic). I'll take a look into TRACE output. – dtrunk Oct 14 '14 at 17:04
  • I've added the TRACE output above. See UPDATE section. – dtrunk Oct 14 '14 at 17:12
  • Just to be sure, you have exported `SPRING_PROFILES_ACTIVE` variable right, otherwise it won't be visible to the child java process..I tried the exact same set up and I am not able to replicate the behavior. – Biju Kunjummen Oct 14 '14 at 17:17
  • @BijuKunjummen I've exported the environment variable by creating a file in `/etc/profile.d` – dtrunk Oct 14 '14 at 17:20
  • Okay, just to be sure, can you please also print - `System.getenv("SPRING_PROFILES_ACTIVE")` in your code, before you list your profiles – Biju Kunjummen Oct 14 '14 at 17:24
  • `DEBUG c.e.WebApplicationInitializer - System.getenv("SPRING_PROFILES_ACTIVE"): null` – dtrunk Oct 14 '14 at 17:30
  • It seems that your Java process can't access your property. Is the property set in .bash_profile or .bashrc? Please make sure that the variable is set in .bashrc. – alain.janinm Oct 14 '14 at 17:47
  • I made a file in /etc/profile.d as mentioned here: https://help.ubuntu.com/community/EnvironmentVariables#System-wide_environment_variables – dtrunk Oct 14 '14 at 17:55
  • You should use bashrc for system wide variable. profile.d is only used for bash login shell. If you start your java process from a non-login shell, then properties on profile.d are not used. – alain.janinm Oct 14 '14 at 18:02

1 Answers1

1

If System.getenv("SPRING_PROFILES_ACTIVE") returns null it's because the variable is not defined in the system environment.

Files in /etc/profile.d/*.sh are only loaded for bash login shells. If you start your Java process from a non-login shell then variable for login shell are not loaded/visible.

Edit :

Because Tomcat is started as a service you have to set the environment variable in setenv.sh.

Community
  • 1
  • 1
alain.janinm
  • 19,951
  • 10
  • 65
  • 112
  • Nope. Still `null`. Neither `/etc/bash.bashrc` nor `~/.bashrc`. – dtrunk Oct 14 '14 at 18:32
  • @dtrunk How are you launching the java app? – alain.janinm Oct 14 '14 at 18:35
  • Tomcat 7 which is running as a service. And yes, after putting the exports in the mentioned files I did a reboot. – dtrunk Oct 14 '14 at 18:54
  • 1
    @dtrunk Ok the pb is the fact that you launch it as a service. Try this, take /opt/tomcat/bin/daemon.sh script and put it in /etc/init.d/tomcat in order to start Tomcat automatically at boot time, then set your environment variables in setenv.sh – alain.janinm Oct 14 '14 at 19:27
  • creating setenv.sh and adding export SPRING_PROFILES_ACTIVE there worked. Thanks! Pls update your answer so I can accept it. – dtrunk Oct 14 '14 at 19:34