Announcement Announcement Module
No announcement yet.
Conflict between <mvc:annotation-driven> and DefaultAnnotationHandlerMapping Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Conflict between <mvc:annotation-driven> and DefaultAnnotationHandlerMapping

    I'm overriding the "default" instantiated DefaultAnnotationHandlerMapping explicitly for registering additional interceptors like this:

    	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    		<property name="alwaysUseFullPath" value="false"/>
    		<property name="interceptors">
    				<ref local="localeChangeInterceptor"/>
    But strangely and according to the logs I end up with actually *2* instances of DefaultAnnotationHandlerMappings. So the implicit (default) one is obviously not replaced by the explicitly defined one (which it should hoever according to the docs).

    I pinned down that the source of this behaviour seems to be in the <mvc:annotation-driven> directive which I have as well in my configuration for using JSR 303 validation with hibernate validator. When I remove this piece of xml the explicit DefaultAnnotationHandlerMapping wins and the default one is gone (plus the interception for the locale works then - it did not before as the implicit DefaultAnnotationHandlerMappings did not have the interceptor registered at all).

    Bug ? Or why should the annotation-driven thing have an impact on changing the override-behavior of the handler mappings ?

    Best Regards,


    PS: I'm talking about MVC 3.0.0 RC2 here....
    Last edited by rauar; Dec 1st, 2009, 01:42 PM. Reason: More info

  • #2
    Alright - as always self-reply...

    I've "unrolled" the annotation-driven directive using the info in this jira ticket here:

    Afterwards I unified my own DefaultAnnotationHandlerMapping incl. its interceptor and the DefaultAnnotationHandlerMapping from the "unrolled" part.

    Works then as it should (no duplicate controller registration anymore, JSR-303 annotation-based validation still working and locales work nicely as well...

    So my conclusion here would be that this is more sort of a bug as the annotations-driven directive defines implicitly a third (!) DefaultAnnotationHandlerMapping which already overrides the implicit one and leaves no chance for my custom DefaultAnnotationHandlerMapping with its interceptors.

    If someone could confirm or clarify this ? Would open a jira ticket then....

    Regards Alex


    • #3
      Yeah, I had the same issue - your post helped me out. Spring should consider this to be a bug, and provide a way in the annotation to set your own AnnotationMethodHandlerAdapter. The current Spring documentation is conflicting and misleading.


      • #4
        Yeah. I added a comment to the corresponding ticket (above) which introduced the short-cut.

        Regards, Alex


        • #5
          Hmm - that looks like a comment to a closed issue (the one in which they implemented annotation-driven), not a new bug report. I think it would be great to file a full bug for this issue...


          • #6
            Absolutely. I have to admit: I was too lazy

            I saw that 3.0.0RC3 has been released in jira so I need to first double-check whether that behaviour is still present in that version (I guess it will).

            Will post back the new ticket id then.



            • #7
              Ok, same behaviour in RC3. Issue is here:

              Feel free to comment on this issue. Not sure whether the explanation is clearly understandable.

              regards Alex


              • #8
                This is definitely a pitfall. I just spent and hour trying to understand why the interceptors are not working. So thanks rauar for self commenting !!


                • #9
                  Related to the original post:

                  If the only reason for explicitly declaring a DefaultAnnotationHandlerMapping bean is to apply interceptors, an alternative is to use <mvc:interceptors> along with <mvc:annotation-drive>.

                    <mvc:annotation-driven />
                      <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
                  This will register the interceptor beans into the default handler mapping.


                  • #10
                    Had the same issue when defining my own AnnotationMethodHandlerAdapter so I could register custom messageConverters. The
                    <mvc:annotation-driven />
                    tag I had used for registering a validator and conversion service created its own AnnotationMethodHandlerAdapter, put it at order #0, and my message converters were never used.

                    Took me a while to track it down to the same thing; took out the
                    <mvc:annotation-driven />
                    tag and defined all my own stuff using this instead:

                    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
                            <property name="webBindingInitializer">
                                <bean class="">
                                    <property name="conversionService" ref="customConversionService"/>
                                    <property name="validator" ref="customValidator"/>
                            <property name="messageConverters">
                    Hope that helps someone not spend an hour or two tracking it down...