0

I'm trying to clear up some Spring concepts here. I have two contextConfigureLocation xml files as defined in web.xml here

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

So, in the first configuraton file "spring-datasource.xml", I did a component-scan like so

<context:annotation-config />
<context:component-scan base-package="com.mycompany.root" />

my source code structure is like so

com
--->mycompany
--------------->root
--------------------->ui
--------------------->server
--------------------->etc

The issue is, my controllers decorated with @controllers under "com.mycompany.root.ui" never got picked up.

in the spring-servlet.xml, I had to do another componet-scan like so

<context:annotation-config />
<context:component-scan base-package="com.mycompany.root.ui" />

<mvc:resources mapping="/images/**" location="/images/" />
<mvc:annotation-driven />
<mvc:default-servlet-handler/>

for my controllers to get picked up.

Why is that? I thought whatever higher up in the parent configuration files should automatically be avaialbe to the children configuraton file? Or is that not the case as evident here.

[EDit] - After some reading, I think I'm more curious with controllers instantiated in the root application context, what happened to them when the spring-dispatch servlet got to them? The child applicaton context just ignored them? It will be nice if anyonne can show me some source code of dispatch servlet that did the ignore.

Thanks

Liming
  • 1,641
  • 3
  • 28
  • 38
  • Do read [this](http://stackoverflow.com/questions/7414794/difference-between-contextannotation-config-vs-contextcomponent-scan) and [this](http://stackoverflow.com/questions/3652090/difference-between-applicationcontext-xml-and-spring-servlet-xml-in-spring) which will explain everthing you seek. – Bond - Java Bond Sep 10 '15 at 05:19
  • Everything from the root is available for the child BUT `` doesn't look for controllers etc. in the parent context only in the current context. With your current setup you actually end up with 2 instances of a controller, one used and another just taking up memory. – M. Deinum Sep 10 '15 at 05:43
  • Thanks Java-Bond and M.Denium. @M.Denium. Good point, I will exclude the controllers in the root config... Still, I mean i'm trying to find the documentation in Spring regarding this specific rule of spring-servlet.xml, only controllers can be scanned here.. why is that? – Liming Sep 10 '15 at 05:55

0 Answers0