我在SPRING_PROFILES_ACTIVE
本地 linux 机器上设置了环境变量:
$ echo $SPRING_PROFILES_ACTIVE
development,develop,devel,dev
在我的 servlet 初始化程序中,我设置了一个默认配置文件,因为我没有通过扩展AbstractDispatcherServletInitializer
和覆盖在我的生产机器上设置任何变量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;
这就是输出:
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,
为什么没有有效的个人资料?
更新
具有 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,
更新 2
我通过在以下位置创建文件导出了环境变量/etc/profile.d
:
DEBUG c.e.WebApplicationInitializer - System.getenv("SPRING_PROFILES_ACTIVE"): null