3

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

first time:

 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]

And the second time:

 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] 

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>

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.

1 Answer 1

1

That's because of 2 contexts

servlet-level spring context

and

global Spring context common to the whole application

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>

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

1
  • 4
    I don't want it that way, cause some beans got instantiated twice though declared singleton.
    – tuan long
    Jan 8, 2015 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.