Spring MVC上下文层次结构



这可能是一个简单的问题,但是我很难理解Spring MVC如何处理MVC应用程序中经常出现的多个层次结构。

在我的web.xml我有一个上下文定义为我的理解是,这是根上下文

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/spring/root-context.xml
    </param-value>
</context-param>

我还在servlet级别定义了一个上下文,我理解它应该继承上面的根上下文。

<servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>

为了使我的应用程序工作,我有以下在root-context.xml

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

和servlet-context.xml

中的以下内容
<annotation-driven />
<context:component-scan base-package="com.demo.controller" />

我的问题是,如果我只在根上下文中更改组件扫描,并从servlet上下文中删除组件扫描,为什么应用程序拒绝加载?

<context:component-scan base-package="com.demo" />
Annotation-specified bean name 'adminController' for bean class [com.demo.controller.AdminController] conflicts with existing, non-compatible bean definition of same name 

因为servlet-context找不到任何控制器,所以根上下文和servlet-context是分开加载的

最新更新