考虑使用弹簧4的基于Web的应用程序。弹簧bean配置文件在web.xml中定义,如:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>prod,edb,cas</param-value>
</context-param>
现在考虑将bean在spring-applicaiton-context.xml中定义为
<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])
}
答案 0 :(得分:2)
语法为"#{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会导致加载属性。