为什么在Spring MVC应用程序中创建两个Spring bean控制器实例? [英] Why two instances of spring bean controllers are created in a Spring MVC application?

查看:131
本文介绍了为什么在Spring MVC应用程序中创建两个Spring bean控制器实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

  1. 为什么这个特定控制器bean的实例数是两个?
  2. 默认情况下,弹簧豆为单例.当然,这里的实例不会随多个请求而变化,而应该是一个.

这是我的配置: web.xml

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文件

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>

和项目结构:

控制器类:

 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";
        }
    }

推荐答案

您正在两次加载上下文.

You are loading the context twice.

  1. 使用调度程序servlet servlet定义.

  1. Using the dispatcherservlet servlet definition.

也使用我在评论中提到的contextloader侦听器. ->您无需执行此步骤.

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

看看这个:

为什么使用上下文加载器侦听器?

这篇关于为什么在Spring MVC应用程序中创建两个Spring bean控制器实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆