在Spring Servlet项目的web.xml中加载contextConfigLocation的顺序 [英] Order of loading contextConfigLocation in web.xml of Spring Servlet project

查看:245
本文介绍了在Spring Servlet项目的web.xml中加载contextConfigLocation的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 Spring Java 项目,我正在尝试将其配置为Web服务器servlet。以下是 web.xml 文件的精简版本:

Suppose that I have a Spring Java project and I am trying to configure it as a web server servlet. Here is a stripped-down version of the web.xml file:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/generalApplicationContext.xml
    </param-value>
</context-param>

<servlet>
    <servlet-name>my-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/specificApplicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>my-servlet</servlet-name>
    <url-pattern>/foo/*</url-pattern>
</servlet-mapping>

这里要注意的关键是我已经指定了两个要加载的XML文件。一个是我的整个应用程序的通用,而另一个是特定于my-servletservlet。对于只有一个servlet映射的设置,这没有意义。但是,我的项目有多个servlet映射,每个都有特定的Spring设置。

The key thing to note here is that I have specified two XML files to be loaded. One is general for my entire application, while the other is specific to the "my-servlet" servlet. For a setup with just one servlet-mapping, this wouldn't make sense. However, my project has multiple servlet-mappings and each one has specific Spring settings to them.

我的问题:哪个contextConfigLocation将是首先由Spring加载?它是generalApplicationContext.xml还是specialApplicationContext.xml?更重要的是,装载的顺序是否重要?从我的调试工作来看,这似乎很明显,因为当我将一些独立的Spring配置从一个文件移动到另一个文件时,我得到了不同的错误。

My Question: Which contextConfigLocation is going to be loaded first by Spring? Will it be the generalApplicationContext.xml or will it be the specificApplicationContext.xml? More importantly, does the order of loading even matter? From my debugging efforts, it seems apparent that it does because I get different errors when I move some independent Spring configuration from one file to the other.

NB: 对于多个servlet映射是否使用多个弹簧配置是一个很好的做法值得商榷。使用XML配置而不是新的Java配置也是如此。但这不是我在这里要问的问题。让我们试着关注我的主要问题。

NB: Whether or not using multiple spring configurations for multiple servlet mappings is a good practice is debatable. Same goes for using XML config instead of the new Java config. But that's not what I'm trying to ask here. Let's try to focus on my main question.

推荐答案

generalApplicationContext.xml 是首先加载的,因为它是 ApplicationContext 加载 ContextLoaderListener

generalApplicationContext.xml is the one that will be loaded first because it is the ApplicationContext loaded with the ContextLoaderListener

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

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/generalApplicationContext.xml
    </param-value>
</context-param>

specificApplicationContext.xml 实际上是一个子上下文上面加载的 generalApplicationContext.xml ,它将是 WebApplicationContext

specificApplicationContext.xml is actually a Child Context of the Above loaded generalApplicationContext.xml and it will be a WebApplicationContext

<servlet>
    <servlet-name>my-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/specificApplicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>my-servlet</servlet-name>
    <url-pattern>/foo/*</url-pattern>
</servlet-mapping>

是的,加载顺序很重要。因为在加载父上下文时,必须满足所有必需的依赖项。

And yes the order of loading does matter. Because when the parent context is loaded all the required dependencies must be fulfilled.

这篇关于在Spring Servlet项目的web.xml中加载contextConfigLocation的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆