使用带有后备的Spring配置文件
作者:互联网
我在我的本地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,
为什么没有活动配置文件?
UPDATE
具有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
解决方法:
如果System.getenv(“SPRING_PROFILES_ACTIVE”)返回null,那是因为该变量未在系统环境中定义.
/etc/profile.d/*.sh中的文件仅为bash登录shell加载.如果从non-login shell启动Java进程,则不会加载/显示登录shell的变量.
> What’s the difference between .bashrc, .bash_profile, and .environment?
> Difference between .bashrc and .bash_profile
编辑:
因为Tomcat是作为服务启动的,所以您必须在setenv.sh中设置环境变量.
标签:spring,spring-profiles 来源: https://codeday.me/bug/20190708/1403238.html