Spring Web 应用程序被初始化两次 [英] Spring Web Application got initialized twice

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

问题描述

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

I found my spring web project got initialized twice on tomcat and here are the messages printed

第一次:

 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 中的内容:

An here is the content in my 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>

最后是我的 mvc-servlet.xml 文件:

and final is my mvc-servlet.xml file:

<?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>

我已经尽我所能在谷歌上找到解决方案,但没有任何效果,谁能帮忙,非常感谢.

I've tried as hard as I can to find a solution on google, but nothing effective, can anyone help, thanks very much.

推荐答案

那是因为 2 个上下文

That's because of 2 contexts

servlet-level spring context

global Spring context common to the whole application

在你的 web.xml 中

In your 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>

另见为什么 Spring MVC 至少需要两个上下文?Spring MVC Web 应用程序:应用程序上下文启动两次

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

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