Spring MVC Web应用程序:应用程序上下文启动两次 [英] Spring MVC web app: application context starts twice

查看:157
本文介绍了Spring MVC Web应用程序:应用程序上下文启动两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a Spring MVC REST API. Everything works fine, which is great, but I noticed from the logs that every time I restart my app the applicationContext loads twice: once when tomcat loads the war file, and a second time when the web app is accessed for the first time by a client.

我举几个例子:

我启动tomcat之后:

Right after I start the tomcat:

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调用:

And then at the moment I do the first API call:

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

当然这不是正常行为吗?我的web.xml如下所示:

Surely this can't be normal behavior?? My web.xml looks like this:

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

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>

推荐答案

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

mvc-dispatcher is loading 2x because that is how you've defined it

<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 /资源放在一起。

the first approach is usually for loading something like a "global" or "root" context, where you might put all the bean/resources shared by multiple servlet contexts.

第二种方法通常用于加载特定的servlet上下文。正如本文中的第一个回答所指出的那样,它使用命名约定来查找mvc-dispatcher配置文件,所以你不要需要明确定义它。

The second approach is usually for loading a specific servlet context. As the first answer in this post points out, it uses naming convention to find the mvc-dispatcher config file, so you don't need to explicitly define it.

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

Do you have everything defined in mvc-dispatcher-servlet.xml? If so you can remove the

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

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

definition, otherwise you can (which I recommend for future maintainability) separate your configuration into multiple files. Then load shared beans/resource in something like a root-context.xml (via the first method), and each servlet specific config under servletname-servlet.xml for each servlet context.

这篇关于Spring MVC Web应用程序:应用程序上下文启动两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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