考虑带有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" />

是否可以访问 Activity 配置文件列表并选择第二个配置文件(在我的示例edb中)。通过这种方式,我可以在 Activity 配置文件更改时动态地加载资源。

这可能有帮助!当Web应用程序以以下代码开头时,我可以获取 Activity 配置文件:
    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会导致属性被加载。

09-09 23:03
查看更多