'spring annotation interceptor'에 해당되는 글 1건

  1. 2011.09.07 [JAVA] Spring Framework Annotation-based Controller Interceptor Configuration

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 theIgnoreSelectedAnnotationHandlerMapping.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'sDefaultAnnotationHandlerMapping 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.



출처 : http://www.scottmurphy.info/spring_framework_annotation_based_controller_interceptors
Posted by 나랑살자
,