2013-04-11 166 views
21

我正在開發Spring MVC REST API。一切正常,這很好,但我從日誌中注意到,每當我重新啓動我的應用程序時,applicationContext都會加載兩次:一次當tomcat加載war文件,第二次當web應用程序第一次被一個客戶。Spring MVC Web應用程序:應用程序上下文啓動兩次

我舉幾個例子:

權後,我啓動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調用:

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看起來是這樣的:

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> 

+3

以下問題沒有選擇答案,但可能仍然有幫助:http://stackoverflow.com/q/11409237/866172 – Jalayn 2013-04-11 14:27:02

+0

給出的第一個答案不適用於我。而第二個答案不適用於我,因爲它不適用於OP – Hendrik 2013-04-11 15:38:59

+0

@Jalayn upvote - 你基本上指出了答案 – ikumen 2013-04-11 17:41:47

回答

22

mvc-dispatcher加載2倍因爲那是你如何定義它

<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上下文。正如第一個answer in this post指出的那樣,它使用命名約定來查找mvc-dispatcher配置文件,因此您不需要明確定義它。

你有沒有在mvc-dispatcher-servlet.xml中定義的所有東西?如果是這樣你可以刪除

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

定義,否則,你可以(我建議爲今後的可維護性)分開配置成多個文件。然後通過類似於root-context.xml的方式(通過第一種方法)和每個servlet上下文的servletname-servlet.xml下的每個servlet特定配置來加載共享bean /資源。

+1

我最終得到它的工作:當我刪除上下文參數的時候,它抱怨說沒有applicationContext.xml。所以我創建了一個,我把我所有的從mvc-dispatcher.xml移到了applicationContext.xml。然後我遇到了一些問題,因爲mvc-dispatcher.xml中缺少一些東西:我必須在那裏添加一個和一個,然後一切正常。 – Hendrik 2013-04-11 18:50:41

+0

我有同樣的問題,並使用以下的配置(與DispatcherServlet的)解決: ' 其餘 org.springframework.web.servlet。DispatcherServlet的 ​​contextConfigLocation的 /WEB-INF/rest-servlet.xml <負載上啓動> 1 ' – 2015-04-06 17:55:24

相關問題