【发布时间】:2014-12-02 23:38:00
【问题描述】:
我有一个简单的 Spring MVC 应用程序,带有一个 jsp 和一个控制器类,部署在一个 tomcat 服务器中。该设置适用于多个请求。我已将控制器类命名为 com.mypackage.mvcController。
现在我使用 jvisualvm 来查找创建此特定控制器类的实例数。它显示 2.
- 为什么这个特定控制器 bean 的实例数是两个?
- 默认情况下,spring bean 是单例的。当然,这里的实例不会随着多个请求而变化,但应该是一个。
这是我的配置: 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 文件
<?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>
以及项目结构:
控制器类:
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";
}
}
【问题讨论】:
-
你能发布你的 web.xml 吗?
-
发布您的配置。
-
您是否加载了两次 bean 定义,可能正在使用 contextConfigLocation 并加载相同的配置文件。
-
我已经给出了上面的web.xml和其他配置。我力图单独加载任何 bean 定义。
标签: spring spring-mvc jvisualvm