[英]Get Spring profile name with spring EL
考虑一个带有spring 4的基于Web的应用程序。spring bean配置文件在web.xml中定义,如下所示:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>prod,edb,cas</param-value>
</context-param>
现在考虑在spring-applicaiton-context.xml中将bean定义为
<util:properties id="myPolicy"
location=
"classpath:/configs/${ACCESS-ACTIVE-PROFILE-SECOND-ITEM}/my-policy.properties" />
是否可以访问活动配置文件列表并选择第二个配置文件(在我的示例edb中)。 这样,当活动配置文件更改时,我可以动态地加载资源。
这可能会有所帮助! 当Web应用程序以以下代码开头时,我可以获取活动配置文件:
public void contextInitialized(ServletContextEvent event){
ApplicationContext applicationContext = WebApplicationContextUtils
.getWebApplicationContext(event.getServletContext());
String activeProfiles[] = applicationContext.getEnvironment().getActiveProfiles();
system.out.print(activeProfiles[1])
}
语法为"#{environment.activeProfiles[1]}"
-但是,在上下文生命周期中为时尚早。 在这种情况下,在评估SpEL之前未设置activeProfiles。
怎么了
<beans profile="foo">
<util:properties id="myPolicy"
location="classpath:/configs/foo/my-policy.properties" />
</beans>
<beans profile="bar">
<util:properties id="myPolicy"
location="classpath:/configs/bar/my-policy.properties" />
</beans>
?
其实我才发现
"#{environment.getActiveProfiles()[1]}"
有效-显式调用getter会导致加载属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.