Why two instances of spring bean controllers are created in a Spring MVC application?

时光毁灭记忆、已成空白 提交于 2019-12-06 09:09:40

问题


I have simple Spring MVC application with a jsp and a controller class, deployed in a tomcat server. The setup works fine for multiple requests. I have named the controller class as com.mypackage.mvcController.

Now I used jvisualvm to find the number of instances this particular controller class is created. It shows 2.

  1. Why number of instances of this particular controller bean is two?
  2. By default spring beans are singleton. Of course here the instances does not vary with multiple requests, but should have been one.

Here is my configuration: web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/WEB-INF/pages/welcome.jsp</welcome-file>
    </welcome-file-list>

</web-app>

mvc-dispatcher-servlet.xml file

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

    <context:component-scan base-package="com.myPackage" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

and the project structure:

controller class:

 package com.myPackage;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;

    @Controller
    @RequestMapping("serverHit")
    public class mvcController {

        @RequestMapping
        public String sayHello() {
            System.out.println("spring test");
            return "result";
        }
    }

回答1:


You are loading the context twice.

  1. Using the dispatcherservlet servlet definition.

  2. Using the contextloader listener as I mentioned in the comment too. -> you don't need to do this step.

Have a look at this:

Why use context loader listener?




回答2:


Spring beans are, by default, "Spring singleton". That means one instance per context. A web application typically has at least two contexts - the root one and the web one. Most likely you have the controller instantiated for both of those. @ComponentScan is the most likely fault - try adding filters that will exclude any controllers from the root context.



来源:https://stackoverflow.com/questions/26254720/why-two-instances-of-spring-bean-controllers-are-created-in-a-spring-mvc-applica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!