在Spring Servlet项目的web.xml中加载contextConfigLocation的顺序

ecb*_*die 19 java spring web.xml servlets spring-mvc

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

sha*_*zin 22

generalApplicationContext.xml是首先ApplicationContext加载的,因为它是加载的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>
Run Code Online (Sandbox Code Playgroud)

specificApplicationContext.xml实际上是上面加载的子上下文,generalApplicationContext.xml它将是一个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>
Run Code Online (Sandbox Code Playgroud)

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

  • 我知道`WebApplicationContext` 是`ApplicationContext` 的一个子类型。但是我想知道`ContextLoaderListener` 究竟创建了什么,`ApplicationContext` 或`WebApplicationContext`。如果它创建了`ApplicationContext`实例,它绝对不等同于`WebApplicationContext`,因为前者不提供后面的功能。就像,准确地说,`ContextLoaderListener` 创建了`WebApplicationContext`,它也提供了`ApplicationContext` 的功能,是它的子类型吗?[继续...] (2认同)
  • [...继续] [这个答案](/sf/answers/827215791/) 说它创建了“ApplicationContext”和“WebApplicationContext”。现在试图从 [source](https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/web/context /ContextLoaderListener.java)。但它没有任何意义...... (2认同)
  • [...继续] 好的看着 [源](https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/web/ context/ContextLoaderListener.java)(也是它的[超类型](https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/web/ context/ContextLoader.java)),它似乎创建了`WebApplicationContext`。解释这个主题的文章为了方便起见说“ApplicationContext”(或者隐含地假设读者会知道我们总是指“WebApplicationContext”)。 (2认同)
  • [...contined 4] 是不是像`ContextLoaderListener`创建的`WebApplicaionContext`是[通常在线文章引用](https://i.postimg.cc/s2CKv8b0/webappcontext.png)作为**“root "** `WebApplicationContext`,只是为了将它与`DispatcherServlet` 创建的`WebApplicationContext` 区分开来? (2认同)

Ank*_*hal 8

下面的部分加载上下文文件并创建ApplicationContext.这个上下文可能,例如,包含诸如,你可能要在应用程序中使用(再利用),中间层交易服务,数据访问对象或其他物体.每个应用程序将有一个应用程序上下文.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/generalApplicationContext.xml
    </param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

另一个上下文是WebApplicationContext,它是应用程序上下文子上下文.DispatcherServletSpring Web应用程序中定义的每个都将具有关联WebApplicationContext.初始化的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>
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅


Sha*_*dra 7

实际拥有Spring调试日志的更好方法是告诉您自己的顺序.如果你想进入代码,你也可以看看org.springframework.web.servlet.FrameworkServlet(DispatcherServlet扩展这个类)只需"org.springframework.web.servlet"在你首选的日志框架中启用logger 调试级别

以下是日志通常的样子 - 显然首先加载根上下文并将其设置为上下文层次结构的父上下文 - 接下来加载servlet上下文.

INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/generalApplicatonContext.xml]
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 256 ms
DEBUG: org.springframework.web.servlet.DispatcherServlet - Initializing servlet 'my-servlet'
INFO :Initializing Spring FrameworkServlet 'appServlet'
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'my-servlet': initialization started
DEBUG: org.springframework.web.servlet.DispatcherServlet - Servlet with name 'appServlet' will try to create custom WebApplicationContext context of class 'org.springframework.web.context.support.XmlWebApplicationContext', using parent context [Root WebApplicationContext: startup date [Fri May 15 17:08:24 IST 2015]; root of context hierarchy
DEBUG: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/specificApplicationContext.xml
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

56536 次

最近记录:

10 年,3 月 前