6

Spring web applications have two context - the web context (child) and the root context (parent). If @Controller beans are defined in dispatcher-servlet.xml (the web context) everything is fine.

But if the controller beans are defined in the parent context (applicationContext.xml), then the controllers are not recognized - i.e. their mappings are not configured, so when you try to open /foo/bar defined in such controller, the path is not found.

The question is: how to make the controller mappings to be parsed no matter where the beans are defined.

P.S. I know I can move the declarations to the child context, but I don't want to do that for reasons beyond the scope of this question.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • related: http://stackoverflow.com/questions/1757098/spring-mvc-annotations-with-global-context-contextcomponent-scan – Bozho Oct 07 '11 at 14:46

1 Answers1

5

DefaultAnnotationHandlerMapping has a detectHandlersInAncestorContexts property that enables the desired behaviour.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • well, I looked through all the classes in the hierarchy for such a property..but obviously I missed it. Thanks :) – Bozho Oct 07 '11 at 14:51
  • now, the harder part is to use the DefaultAnnotationHandlerMapping together with mvc:annotation-driven – Bozho Oct 07 '11 at 14:52
  • @Bozho: I always use `BeanPostProcessor` to configure implicitly created beans in such cases. – axtavt Oct 07 '11 at 14:56
  • 1
    @Bozho: A more interesting thing is that Spring 3.1's `RequestMappingHandlerMapping`, that is intended to be a drop-in replacement for `DefaultAnnotationHandlerMapping`, doesn't have such a property. Perhaps it should be reported. – axtavt Oct 07 '11 at 15:00
  • That's a good idea. For bigger projects I "unfold" the annotation-driven tag (and remove it), but this would be an overhead here. I'll use a post-processor. – Bozho Oct 07 '11 at 15:00