代码之家  ›  专栏  ›  技术社区  ›  shsteimer

是否基于app war文件外部指定的属性加载变量spring applicationContext.xml文件?

  •  1
  • shsteimer  · 技术社区  · 14 年前

    我需要更改基于属性this属性使用的spring applicationContext.xml文件 必须

    3 回复  |  直到 14 年前
        1
  •  2
  •   Michael Wiles    14 年前

    你考虑过使用beanRefContext方法吗。(ContextSingletonBeanFactoryLocator)。这样,您可以通过另一个spring配置文件来配置spring配置文件(及其名称)。

    文件如下所示:

    <beans>
        <bean id="businessBeanFactory" class="org.springframework.context.support.ClassPathXmlApplicationContext">
            <constructor-arg value="${NameOfBeanConfigFile}" />
        </bean>
    </beans>
    

    并且可以使用PropertyPlaceHolderConfigurer设置beanconfigfile的NameOfBeanConfigFile的值。

    我喜欢这种方法,因为它意味着我可以混合静态bean配置文件名和动态bean配置文件名,因此不必重复bean配置。

    当我不得不做类似的事情时,我会通过加载为URL资源的配置文件(通过jndi)进行参数化

        2
  •  2
  •   shsteimer    14 年前

    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
    
        <context-param>
            <param-name>contextConfigLocation1</param-name>
            <param-value>classpath:applicationContext-1.xml</param-value>
        </context-param>
    
        <context-param>
            <param-name>contextConfigLocation2</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
    
        <listener>
            <listener-class>com.my.package.MyContextLoaderListener</listener-class>
        </listener>
    

    然后我扩展ContextLoaderListener

    package com.my.package;
    
    import org.springframework.web.context.ContextLoader;
    import org.springframework.web.context.ContextLoaderListener;
    
    public class MyContextLoaderListener extends ContextLoaderListener {
        @Override
        protected ContextLoader createContextLoader() {
            return new MyContextLoader();
        }
    }
    

    和ContextLoader

    package com.my.package;
    
    import javax.servlet.ServletContext;
    
    import org.springframework.web.context.ConfigurableWebApplicationContext;
    import org.springframework.web.context.ContextLoader;
    
    public class LnvContextLoader extends ContextLoader {
    
        private static final String APP_CONTEXT_PROP = "MY_CONTEXT_LOAD_PARAM";
    
        @Override
        protected void customizeContext(ServletContext servletContext,
                ConfigurableWebApplicationContext wac) {
    
            //check for system property first, if not defined, check for env variable
            String appContextParam = System.getProperty(APP_CONTEXT_PROP);
            if(appContextParam==null)
            {
                appContextParam = System.getenv(APP_CONTEXT_PROP);
            }
    
    
            if(appContextParam!=null && !appContextParam.equals("")){
    
                String initParam = servletContext.getInitParameter(appContextParam);
    
                wac.setConfigLocation(initParam);
            }
    
    
        }
    }
    

    set MY_CONTEXT_LOAD_PARAM=contextConfigLocation1
    

    此解决方案从环境变量加载它,但代码是灵活的,允许在系统属性中设置它。

        3
  •  -2
  •   Guillaume    14 年前

    我的解决方案是使用一个非常简单的application context,它包含真正的应用程序上下文:

    applicationContext.xml:

    <beans>
        <import resource="classpath:realContext.xml"/>
    </beans>
    

    <beans>
        <import resource="classpath:realContext2.xml"/>
    </beans>
    

    并在WAR中打包realContext.xml和realContext2.xml。不需要花哨的上下文侦听器。

    只是我的意见,但我很不喜欢战争不是自给自足的。我发现只有一个部署单位是非常方便的。因此,我希望在构建过程中创建两个不同版本的WAR,每个版本对应一个所需的配置。

    另一种解决方案是,如果要根据给定的属性加载不同的bean,可以使用propertyplaceholderconfig并将bean的名称作为属性:

    <beans>
      <bean id="bean1" .../>
      <bean id="bean2" .../>
      <bean id="otherBean">
        <property name="injectDifferentBean" ref="${property.containing.bean.name" />
      </bean>
    </beans>
    

    以及具有以下内容的属性文件:

    property.containing.bean.name=bean1
    

    property.containing.bean.name=bean2