Announcement Announcement Module
Collapse
No announcement yet.
Conflict between <mvc:annotation-driven> and DefaultAnnotationHandlerMapping Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
    	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    		<property name="alwaysUseFullPath" value="false"/>
    		<property name="interceptors">
    			<list>
    				<ref local="localeChangeInterceptor"/>
    			</list>
    		</property>
    	</bean>
    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,

    Alex

    PS: I'm talking about MVC 3.0.0 RC2 here....
    Last edited by rauar; Dec 1st, 2009, 12: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: https://jira.springsource.org/browse/SPR-6306

    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

    Comment


    • #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.

      Comment


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

        Regards, Alex

        Comment


        • #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...

          Comment


          • #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.

            Regards,
            Alex

            Comment


            • #7
              Ok, same behaviour in RC3. Issue is here: https://jira.springsource.org/browse/SPR-6524

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

              regards Alex

              Comment


              • #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 !!

                Comment


                • #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>.

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

                  Comment


                  • #10
                    Had the same issue when defining my own AnnotationMethodHandlerAdapter so I could register custom messageConverters. The
                    Code:
                    <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
                    Code:
                    <mvc:annotation-driven />
                    tag and defined all my own stuff using this instead:


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

                    Comment

                    Working...
                    X