【问题标题】:Why two instances of spring bean controllers are created in a Spring MVC application?为什么在 Spring MVC 应用程序中创建两个 Spring bean 控制器实例?
【发布时间】:2014-12-02 23:38:00
【问题描述】:

我有一个简单的 Spring MVC 应用程序,带有一个 jsp 和一个控制器类,部署在一个 tomcat 服务器中。该设置适用于多个请求。我已将控制器类命名为 com.mypackage.mvcController。

现在我使用 jvisualvm 来查找创建此特定控制器类的实例数。它显示 2.

  1. 为什么这个特定控制器 bean 的实例数是两个?
  2. 默认情况下,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


【解决方案1】:

默认情况下,Spring bean 是“Spring 单例”。这意味着每个上下文一个实例。 Web 应用程序通常至少有两个上下文 - 根上下文和 Web 上下文。很可能您已经为这两个实例化了控制器。 @ComponentScan 是最有可能的错误 - 尝试添加过滤器,将任何控制器从根上下文中排除。

【讨论】:

【解决方案2】:

您正在加载上下文两次。

  1. 使用 dispatcherservlet servlet 定义。

  2. 使用我在评论中提到的上下文加载侦听器。 -> 你不需要做这一步。

看看这个:

Why use context loader listener?

【讨论】:

  • 谢谢你。它得出结论,当使用 dispathcer servlet 时,不需要上下文加载器侦听器。我删除了侦听器,现在它只显示对象的一个​​实例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-06
  • 2014-07-16
  • 1970-01-01
  • 2021-09-03
  • 1970-01-01
相关资源
最近更新 更多