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.
- Why number of instances of this particular controller bean is two?
- 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";
}
}
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.
You are loading the context twice.
Using the dispatcherservlet servlet definition.
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?