Spring MVC Context Hiearchy

2019-07-28 04:38发布

This is probably a simple question, but I am having some difficulty understanding how Spring MVC handles the multiple hiearchies that are often present in an MVC application.

In my web.xml I have a context defined as My understanding is that this is the Root Context

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

I also have a context defined at the servlet level, which I understand should inherit beens from the root context above.

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

In order to make my app work I have the following in root-context.xml

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

and the following in servlet-context.xml

<annotation-driven />
<context:component-scan base-package="com.demo.controller" />

My question is this, if I change component scan to the following in the root context only and remove the component scan from the servlet context, why does the app refuse to load?

<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 

1条回答
三岁会撩人
2楼-- · 2019-07-28 05:02

because the servlet-context wont find any controller, the root-context and the servlet-context are loaded separately

查看更多
登录 后发表回答