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