使用带有后备的Spring配置文件

时间:2014-10-14 16:07:09

标签: spring spring-profiles

我在本地linux机器上设置了环境变量SPRING_PROFILES_ACTIVE

$ 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

1 个答案:

答案 0 :(得分:1)

如果System.getenv("SPRING_PROFILES_ACTIVE")返回null,那是因为该变量未在系统环境中定义。

/etc/profile.d/*.sh中的文件仅为bash登录shell加载。如果从non-login shell启动Java进程,那么登录shell的变量不会加载/可见。

修改:

因为Tomcat是作为服务启动的,所以你必须在setenv.sh中设置环境变量。

相关问题