简体   繁体   中英

spring mvc tag configuration

1, In spring 3.0 documentation, about mvc:annotation-driven is:

"this tag registers the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans that are required for Spring MVC to dispatch requests to @Controllers."

but i haven't injected this tag into any spring configuration file, i am sure of that.
so why my app can dispatch requests to @Controllers without that?

2, After mvc:default-servlet-handler be injected into my app to handle static resource, all controllers don't work just 404 not found error on web page but static resource is fine.

i goolged it found that maybe a mvc:annotation-driven Lost.

unfortunately a customized interceptor stop working after a mvc:annotation-driven added. here is interceptor definition:

<bean id="currentMemberInterceptor"  class="com.skill.common.CurrentMemberInjectionInterceptor" />

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">  
    <property name="interceptors">  
        <list>
            <ref bean="currentMemberInterceptor" />
        </list>  
    </property>  
</bean>

Interceptor can works if interceptor definition change to mvc tag such as:

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/" />
        <bean class="com.skill.common.CurrentMemberInjectionInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>

I cannot make sense of those tags and relationship after read spring reference.

plz help, thanks!

The thing is that Spring uses default fallback configuration ( default strategies ) in many places when you don't provide any configuration.

This is also true for handler mappings and adapters . If you don't have any of these on your application context, Spring just tries to register some default beans. However as soon as you define at least one bean of the matching type, it will not try to register any defaults and you are on your own (and that makes sense if you think about it).

See DispatcherServlet dependency initialization code and check the default fallback configuration to see what are the defaults.


Configuration namespaces can be sometimes a little bit cryptic about their inner workings. When I don't understand what is some tag doing I usually check source code of the tag handler (naming convention for these classes is "NameOfTheTag"BeanDefinitionParser ). You can try it yourself by checking AnnotationDrivenBeanDefinitionParser (the class behind <mvc:annotation-driven /> ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM