【问题标题】:How to conditionally add(or modify) ContextConfigLocation to web.xml?如何有条件地将 ContextConfigLocation 添加(或修改)到 web.xml?
【发布时间】:2014-04-25 07:24:50
【问题描述】:

我需要在检查一些参数后修改我的项目中的web.xml。我在 web.xml 中的 contextConfigLocation 参数如下:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
  classpath:*/test-spring-config.xml classpath:*/someContext.xml classpath:applicationContext.xml classpath:databaseContext.xml</param-value>
</context-param>

现在我需要在我的应用程序加载到 tomcat 之前检查一些条件,将 classpath:securityContext.xml 添加到 web.xml 中的 contextConfigLocation 中。 我已经尝试在我的 applicationInitializer 类中执行此操作,该类实现 ServletContextListener 如下(显示了部分代码):

public class ApplicationInitializer implements ServletContextListener
{ 
   public void contextInitialized(ServletContextEvent event)
{ ServletContext sc = event.getServletContext();
      if(someConditionIsTrue){
        XmlWebApplicationContext appContext = new XmlWebApplicationContext();
        appContext.setConfigLocation("classpath:securityContext.xml");
        appContext.setServletContext(sc);
        appContext.refresh();
        sc.setAttribute("org.springframework.web.context.WebApplicationContext.ROOT",appContext);
}

但这不起作用,因为我再次将 web.xml 中的上下文加载为

<listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

有人可以建议如何处理这个问题吗?任何建议表示赞赏。

【问题讨论】:

    标签: java spring jakarta-ee web-applications web.xml


    【解决方案1】:

    经过几天的努力,我能够通过创建一个扩展 ContextLoaderListener 并覆盖其 createContextLoader 方法的 CustomContextLoaderListener 类来做到这一点现在返回一个 customContextLoader 如下:

    @Override
    protected ContextLoader createContextLoader(){
    return new CustomContextLoader();
    }
    

    CustomContextLoader 类扩展了 ContextLoader,我重写了它的 customizeContext 方法,如下所示:

    @Override
    protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext){
    super.customizeContext(servletContext, applicationContext);
    String[] oldLocations = applicationContext.getConfigLocations();
    String[] newLocations;
    if(conditionIsTrue){
    newLocations = new String[oldLocations.length+1];
    for (int i = 0; i < oldLocations.length; i++) {
            newLocations[i] = oldLocations[i];
        }
    newLocations[oldLocations.length] ="classpath:securityContext.xml";
    }else{
        newLocations = new String[oldLocations.length];
        newLocations = Arrays.copyOf(oldLocations, oldLocations.length);
    }
    applicationContext.setConfigLocations(newLocations);
    }
    

    不要忘记在您的 web.xml 中添加 customContextLoaderListener 类。

    <listener>
    <listener-class>com.abc.px.customContextLoaderListener</listener-class>
    </listener>
    

    就是这样!这样我可以有条件地在 web.xml 中设置我的上下文。希望这对某人有帮助!

    【讨论】:

    • 我相信这就是我在回答中提到的;)
    • 是的!我从您的回答中挖掘了上面的所有内容。谢谢老哥!
    【解决方案2】:

    ApplicationInitializer 应该为您的场景扩展 ContextLoaderListener,并且监听器类应该指向 web.xml 中的 ApplicationInitializer

    【讨论】:

      猜你喜欢
      • 2013-06-07
      • 2018-07-19
      • 1970-01-01
      • 2019-02-03
      • 2017-01-02
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      • 2019-05-14
      相关资源
      最近更新 更多