Spring MVC:为什么< mvc:interceptors>声明工作,而不是传统的XML? [英] Spring MVC: Why does this <mvc:interceptors> declaration work, but not the traditional XML?

查看:48
本文介绍了Spring MVC:为什么< mvc:interceptors>声明工作,而不是传统的XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加一些 HandlerInterceptorAdaptor .我将它们添加到我的servlet XML文件中.

我不明白的原因是为什么下面的< mvc:interceptors> 块可以正常工作,而不是传统的带有 DefaultAnnotationHandlerMapping 的bean声明./p>

以下是有效的XML:

 < mvc:interceptors>< bean name ="interceptor1" class ="com.foo.bar"/>< bean name ="interceptor2" class ="com.foo.bar2"/></mvc:interceptors> 



这是我无法使用的XML:

 < bean name ="interceptor1" class ="com.foo.bar"/>< bean name ="interceptor2" class ="com.foo.bar2"/>< bean class ="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"><属性名称="order"值="0"/><属性名称=拦截器">< list>< ref bean ="interceptor1"/>< ref bean ="interceptor2"/></list></property></bean> 

我以为Spring会自动获取我的 DefaultAnnotationHandlerMapping 类型的bean,但事实并非如此.

请注意,我使用 @Controller 注释了我所有的Controller类,并使用 @RequestMapping 注释了Controller中的方法.

有什么想法吗?

解决方案

Spring实际上正在为每个bean(org.springframework.web.servlet.handler.MappedInterceptor)构造一个"MappedInterceptor".

当我想了解Spring NamespaceHandler时,我发现查看源代码(对于NamespaceHandler然后是BeanDefinitionParser)至关重要.

MvcNamespaceHandler指向我指向此InterceptorsBeanDefinitionParser ...

 < bean name ="interceptor1" class ="com.foo.bar"/>< bean name ="interceptor2" class ="com.foo.bar2"/>< bean class ="org.springframework.web.servlet.handler.MappedInterceptor">< constructor-arg index ="0">< null/></constructor-arg>< constructor-arg index ="1">< ref bean ="interceptor1"/></constructor-arg></bean>< bean class ="org.springframework.web.servlet.handler.MappedInterceptor">< constructor-arg index ="0">< null/></constructor-arg>< constructor-arg index ="1">< ref bean ="interceptor2"/></constructor-arg></bean> 

这几乎肯定需要修修补补,但它使您更接近Spring的工作...

尝试一下,如果第一次不能使用,请仔细看一下上面链接到的源代码.

HTH

I'm trying to add some HandlerInterceptorAdaptors to ALL of my Controllers/Actions in Spring MVC. I'm adding these to my servlet XML file.

What I don't understand, is why the <mvc:interceptors> block I have below is working, but not the traditional bean declaration with DefaultAnnotationHandlerMapping.

Here is the XML that is working:

<mvc:interceptors>
    <bean name="interceptor1" class="com.foo.bar" />
    <bean name="interceptor2" class="com.foo.bar2" />
</mvc:interceptors>



Here is the XML that I can't get to work:

<bean name="interceptor1" class="com.foo.bar" />
<bean name="interceptor2" class="com.foo.bar2" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="order" value="0" />
    <property name="interceptors">
        <list>
            <ref bean="interceptor1"/>
            <ref bean="interceptor2"/>
        </list>
    </property>
</bean>

I thought that Spring would automatically pick up my bean of type DefaultAnnotationHandlerMapping, but that doesn't seem to be the case.

Note that I'm annotating all of my Controller classes with @Controller and the methods within the Controller with @RequestMapping.

Any thoughts?

解决方案

Spring is actually constructing one "MappedInterceptor" for each bean (org.springframework.web.servlet.handler.MappedInterceptor).

When I want to understand a Spring NamespaceHandler, I find it crucial to look at the source code (for the NamespaceHandler and then the BeanDefinitionParser).

The MvcNamespaceHandler pointed me to this InterceptorsBeanDefinitionParser ...

http://javasourcecode.org/html/open-source/spring/spring-3.0.5/org/springframework/web/servlet/config/InterceptorsBeanDefinitionParser.java.html

This indicates that you should be building a bean for each 'interceptor', a little bit like this...

<bean name="interceptor1" class="com.foo.bar" />
<bean name="interceptor2" class="com.foo.bar2" />
<bean class="org.springframework.web.servlet.handler.MappedInterceptor">
    <constructor-arg index="0">
        <null />
    </constructor-arg>
    <constructor-arg index="1">
        <ref bean="interceptor1"/>
    </constructor-arg>
</bean>
<bean class="org.springframework.web.servlet.handler.MappedInterceptor">
    <constructor-arg index="0">
        <null />
    </constructor-arg>
    <constructor-arg index="1">
        <ref bean="interceptor2"/>
    </constructor-arg>
</bean>

This will almost-definitely need some tinkering, but it gets you a lot closer to what Spring is doing ...

Give it a try, and if it doesn't work first-time, look harder at the source code I've linked to above.

HTH

这篇关于Spring MVC:为什么&lt; mvc:interceptors&gt;声明工作,而不是传统的XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆