春季 MVC Web 应用程序:应用程序上下文启动两次

2022-09-01 21:03:50

我正在开发一个Spring MVC REST API。一切正常,这很好,但我从日志中注意到,每次我重新启动应用程序时,应用程序Context都会加载两次:一次是当tomcat加载war文件时,第二次是当客户端第一次访问Web应用程序时。

我举几个例子:

在我开始雄猫之后:

Apr 11, 2013 10:14:35 AM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.32
Apr 11, 2013 10:14:36 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
2013-04-11 10:14:36 INFO  ContextLoader:273 - Root WebApplicationContext:     initialization started
2013-04-11 10:14:36 INFO  XmlWebApplicationContext:510 - Refreshing Root     WebApplicationContext: startup date [Thu Apr 11 10:14:36 EDT 2013]; root of context hierarchy
2013-04-11 10:14:36 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions     from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
2013-04-11 10:14:36 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [config-general.xml]
2013-04-11 10:14:37 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [config-db.xml]
2013-04-11 10:14:37 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [config-security.xml]
2013-04-11 10:14:37 INFO  SpringSecurityCoreVersion:33 - You are running with Spring Security Core 3.1.3.RELEASE
2013-04-11 10:14:37 INFO  SecurityNamespaceHandler:59 - Spring Security 'config' module version is 3.1.3.RELEASE

...

然后现在我执行第一个 API 调用:

INFO: Initializing Spring FrameworkServlet 'mvc-dispatcher'
2013-04-11 10:15:25 INFO  DispatcherServlet:455 - FrameworkServlet 'mvc-dispatcher': initialization started
2013-04-11 10:15:25 INFO  XmlWebApplicationContext:510 - Refreshing WebApplicationContext for namespace 'mvc-dispatcher-servlet': startup date [Thu Apr 11     10:15:25 EDT 2013]; parent: Root WebApplicationContext
2013-04-11 10:15:25 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
2013-04-11 10:15:25 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [config-general.xml]
2013-04-11 10:15:25 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [config-db.xml]
2013-04-11 10:15:25 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [config-security.xml]
2013-04-11 10:15:25 INFO  SecurityNamespaceHandler:59 - Spring Security 'config' module version is 3.1.3.RELEASE

当然,这不可能是正常行为??我的网页.xml看起来像这样:

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd“>

<display-name>REST API</display-name>

<!-- Servlets -->
<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<!-- filters -->
<filter>
    <filter-name>httpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>httpMethodFilter</filter-name>
    <servlet-name>mvc-dispatcher</servlet-name>
</filter-mapping>
<filter>
    <filter-name>etagFilter</filter-name>
    <filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>etagFilter</filter-name>
    <servlet-name>mvc-dispatcher</servlet-name>
</filter-mapping>
<filter>
    <filter-name>CompressingFilter</filter-name>
    <filter-class>com.planetj.servlet.filter.compression.CompressingFilter</filter-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>statsEnabled</param-name>
        <param-value>false</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CompressingFilter</filter-name>
    <servlet-name>mvc-dispatcher</servlet-name>
</filter-mapping>
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>



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


答案 1

mvc-dispatcher正在加载 2x,因为这是您定义它的方式

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

第一种方法通常用于加载类似“全局”或“根”上下文的内容,您可以在其中放置由多个 servlet 上下文共享的所有 Bean/资源。

第二种方法通常用于加载特定的 servlet 上下文。正如本文中的第一个答案所指出的那样,它使用命名约定来查找 mvc 调度程序配置文件,因此您无需显式定义它。

您是否在 mvc-dispatcher-servlet 中定义了所有内容.xml?如果是这样,您可以删除

<context-param>
  ..
</context-param>

定义,否则你可以(我建议将来可维护性)将你的配置分成多个文件。然后加载共享的bean/resource,如根上下文.xml(通过第一种方法),并且每个servlet特定配置在servletname-servlet.xml下为每个servlet上下文。


答案 2

推荐