Spring Framework Annotation-based Controller Interceptor Configuration
from http://www.scottmurphy.info/spring_framework_annotation_based_controller_interceptors
In order to use interceptors with Annotation-based controllers, you need to configure the DefaultAnnotationHandlerMapping used by the Spring container.
<bean id="annotationMapper" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <ref bean="myInterceptor1"/> <ref bean="myInterceptor2"/> <ref bean="myInterceptor3"/> </list> </property> </bean>
URL Specific Interceptors for Annotation-based controllers
Unfortunately, using the DefaultAnnotationHandlerMapping for interceptors configures the interceptors for all defined annotation based controllers.
In some cases, you might want to have interceptors apply only to specific controllers.
I have created two HandlerMappings that will accomplish this task:
The SelectedAnnotationHandlerMapping and the IgnoreSelectedAnnotationHandlerMapping.java classes which can both be downloaded from www.springplugins.org
SelectedAnnotationHandlerMapping
The SelectedAnnotationHandlerMapping allows you to specify which urls will be interecepted. It is configured as follows:
<bean id="publicMapper" class="org.springplugins.web.SelectedAnnotationHandlerMapping"> <property name="urls"> <list> <value>/interceptOnly.do</value> </list> </property> <property name="interceptors"> <list> <ref bean="myInterceptor"/> </list> </property> </bean>
The above configuration causes all requests to /interceptOnly.do to be intercepted by myInterceptor.
IgnoreSelectedAnnotationHandlerMapping.java
The IgnoreSelectedAnnotationHandlerMapping allows you to specify which urls will not be interecepted. This is similar to Spring’s DefaultAnnotationHandlerMapping in that it will map all Annotation based controllers except it will not map the defined urls. It is configured as follows:
<bean id="publicMapper" class="org.springplugins.web.IgnoreSelectedAnnotationHandlerMapping"> <property name="order"> <value>0</value> </property> <property name="urls"> <list> <value>/doNotIntercept.do</value> </list> </property> <property name="interceptors"> <list> <ref bean="myInterceptor"/> </list> </property> </bean>
Here all annotation-based controllers will be intercepted with myInterceptor except the one usign /doNotIntercept.do url.
Notice that this interceptor specifies the order attribute. This is necessary because if you are using this controller, you are most likely using another mapper for the specified urls. The order was set to 0 here because it is assumed that the mapping to /doNotIntercept.do will be defined with an order > 0.
-
Articles
- February 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
-
Meta



