Spring Web Application初始化了两次

时间:2015-01-08 07:52:17

标签: java spring spring-mvc annotations servlet-filters

我发现我的spring web项目在tomcat上初始化了两次,这里是打印的消息

第一次:

 INFO: Initializing Spring root WebApplicationContext
 INFO 2015-01-08 15:18:04 ContextLoader Root WebApplicationContext: initialization started 
 INFO 2015-01-08 15:18:04 XmlWebApplicationContext Refreshing Root WebApplicationContext: startup date [Thu Jan 08 15:18:04 GMT+08:00 2015]; root of context hierarchy 
 INFO 2015-01-08 15:18:04 XmlBeanDefinitionReader Loading XML bean definitions from file [/home/pinkdahlia/develop/apache-tomcat-7.0.55/webapps/vips-mobile-mlisting-backend/WEB-INF/classes/spring-shiro.xml] 
 INFO 2015-01-08 15:18:04 XmlBeanDefinition Reader Loading XML bean definitions from file [/home/pinkdahlia/develop/apache-tomcat-7.0.55/webapps/vips-mobile-mlisting-backend/WEB-INF/classes/spring-datasources.xml] 
 INFO 2015-01-08 15:18:04 XmlBeanDefinitionReader Loading XML bean definitions from file [/home/pinkdahlia/develop/apache-tomcat-7.0.55/webapps/vips-mobile-mlisting-backend/WEB-INF/classes/spring-commons.xml] 
 INFO 2015-01-08 15:18:05 XmlBeanDefinitionReader Loading XML bean definitions from file [/home/pinkdahlia/develop/apache-tomcat-7.0.55/webapps/vips-mobile-mlisting-backend/WEB-INF/classes/spring-mybatis.xml] 
 INFO 2015-01-08 15:18:05 XmlBeanDefinitionReader Loading XML bean definitions from file [/home/pinkdahlia/develop/apache-tomcat-7.0.55/webapps/vips-mobile-mlisting-backend/WEB-INF/classes/spring-xmemcached.xml] 
 INFO 2015-01-08 15:18:05 XmlBeanDefinitionReader Loading XML bean definitions from file [/home/pinkdahlia/develop/apache-tomcat-7.0.55/webapps/vips-mobile-mlisting-backend/WEB-INF/classes/spring-aop.xml] 
 INFO 2015-01-08 15:18:05 PropertiesFactoryBean Loading properties file from URL [file:/home/pinkdahlia/develop/config/BackendConf.properties]

第二次:

 INFO: Initializing Spring root WebApplicationContext
 INFO 2015-01-08 15:18:13 ContextLoader Root WebApplicationContext: initialization started 
 INFO 2015-01-08 15:18:13 XmlWebApplicationContext Refreshing Root WebApplicationContext: startup date [Thu Jan 08 15:18:13 GMT+08:00 2015]; root of context hierarchy 
 INFO 2015-01-08 15:18:13 XmlBeanDefinitionReader Loading XML bean definitions from file [/home/pinkdahlia/develop/apache-tomcat-7.0.55/webapps/vips-mobile-mlisting-backend/WEB-INF/classes/spring-shiro.xml] 
 INFO 2015-01-08 15:18:13 XmlBeanDefinitionReader Loading XML bean definitions from file [/home/pinkdahlia/develop/apache-tomcat-7.0.55/webapps/vips-mobile-mlisting-backend/WEB-INF/classes/spring-datasources.xml] 
 INFO 2015-01-08 15:18:13 XmlBeanDefinitionReader Loading XML bean definitions from file [/home/pinkdahlia/develop/apache-tomcat-7.0.55/webapps/vips-mobile-mlisting-backend/WEB-INF/classes/spring-commons.xml] 
 INFO 2015-01-08 15:18:13 XmlBeanDefinitionReader Loading XML bean definitions from file [/home/pinkdahlia/develop/apache-tomcat-7.0.55/webapps/vips-mobile-mlisting-backend/WEB-INF/classes/spring-mybatis.xml] 
 INFO 2015-01-08 15:18:13 XmlBeanDefinitionReader Loading XML bean definitions from file [/home/pinkdahlia/develop/apache-tomcat-7.0.55/webapps/vips-mobile-mlisting-backend/WEB-INF/classes/spring-xmemcached.xml] 
 INFO 2015-01-08 15:18:13 XmlBeanDefinitionReader Loading XML bean definitions from file [/home/pinkdahlia/develop/apache-tomcat-7.0.55/webapps/vips-mobile-mlisting-backend/WEB-INF/classes/spring-aop.xml] 
 INFO 2015-01-08 15:18:14 PropertiesFactoryBean Loading properties file from URL [file:/home/pinkdahlia/develop/config/BackendConf.properties] 

这是我的web.xml中的内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0"
         metadata-complete="true">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:spring-*.xml
        </param-value>
    </context-param>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>

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

    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>  
        <filter-name>shiroFilter</filter-name>  
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
        <init-param>  
            <param-name>targetFilterLifecycle</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  

    <filter-mapping>  
        <filter-name>shiroFilter</filter-name>  
        <url-pattern>/*</url-pattern> 
    </filter-mapping>

    <servlet>
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    </web-app>

和final是我的mvc-servlet.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
                       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <aop:config proxy-target-class="true"></aop:config>

    <context:property-placeholder location="file:${vips.backend.config}"/>

    <context:component-scan base-package="com.vip.mlisting" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

    <mvc:annotation-driven>
        <mvc:argument-resolvers>
            <bean class="com.vip.mlisting.system.resolver.CurrentUserMethodArgumentResolver"/>
        </mvc:argument-resolvers>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

        <mvc:resources mapping="/static/**" location="/WEB-INF/static/"/>

    <!-- the prefix and suffix of ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="1">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="contentType" value="text/html"/>
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
</beans>

我已尽力在谷歌上找到解决方案,但没有任何效果,任何人都可以提供帮助,非常感谢。

1 个答案:

答案 0 :(得分:1)

那是因为2个情境

servlet-level spring context

global Spring context common to the whole application

在您的web.xml中

<servlet>
    <servlet-name>mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

另见 Why does Spring MVC need at least two contexts? Spring MVC web app: application context starts twice