2

ApplicationContext is a parent of WebApplicationContext .

But why and must palce in the same context then it will works ?

Here is my explain:

=============

I config my applicationConext.xml like this :

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

and none similar settings in dispatcher-servlet.xml.

It works!!!

===========

I config my dispatcher-servlet.xml like this :

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

and none similar settings in applicationConext.xml.

It also works!!!

==========

BUT , If i put <context:component-scan base-package="com.github" /> in applicationConext.xml and <mvc:annotation-driven /> in dispatcher-servlet.xml OR put <context:component-scan base-package="com.github" /> in dispatcher-servlet.xml and <mvc:annotation-driven /> in applicationConext.xml.

It doesnot work!!! return httpStatus 404,not found,that means it doesnot mapping to the Controller.

ApplicationContext is a parent of WebApplicationContext , the child context shoud inherit from the parent conext , so I thought the settings can separate in different context but not the same one !

Am I wrong ? Please anybody could answer me , thx !

0915240
  • 43
  • 5

1 Answers1

1

In Spring you have the root application context which is an WebApplicationContext and at least one child WebApplicationContext, although you could have multiple DispatcherServlets with corresponding WebApplicationContexts. Those are different contexts, although the DispatcherServlet's WebApplicationContext is a child of the root application context. This means that the child context has access to all the beans of the root application context, but still they are two different contexts.

Now each context is constructed separately using the corresponding configuration files. Thus if you only specify <context:component-scan /> in one package and <mvc-annoation-driven /> in another, then when constructing the one context it will make a component-scan, but it will not register any controller related annotations since you did not enable them with <mvc-annoation-driven /> in the same config file. Now in the other context, controller related annoations are enabled, but no components are ever scanned. This is why no mappings are found.

This is also why common configuration that would be used by multiple DispatcherServlets is configured in the root application context while controller related configuration is configured in the corresponding web application contexts.

dudel
  • 684
  • 5
  • 15
  • so what does the parent-child context relation means ? only inherit beans from the parent ? – 0915240 Nov 05 '16 at 12:29
  • " Now in the other context, controller related annoations are enabled, but no components are ever scanned. This is why no mappings are found." --- but I think the components can inherit from the parent context, is this right? – 0915240 Nov 05 '16 at 12:29
  • Yes, the web application contexts inherit the beans from the parent root application context, but they are not the same context. – dudel Nov 05 '16 at 12:31
  • As I wrote, since the contexts are created separately you have to enable controller related annotations with `` and also enable component-scanning in the same context, otherwise you only enable controller related annotations without actually scanning the components in one context, while in the other context you scan the components but since you did not enable controller related annotations there, it won't find the controller mappings when scanning. – dudel Nov 05 '16 at 12:33
  • You're welcome. One more thing for clarification: The contexts are constructed separately and the child contexts do not inherit any configuration files from the root contexts, only the constructed beans from the root once the root is configured. That is why you cannot have the component-scan and annotation-driven in different config files. The child configuration file does not inherit anything from the root configuration file. – dudel Nov 05 '16 at 12:55