Using Spring Handler Interceptors to Decouple Business Logic

Consider a Spring MVC application deployed in Tomcat servlet container. For each request, the following sequence of steps occur:

  • Tomcat gets the request and executes the configured chain of servlet filters
  • Spring Dispatcher Servlet receives the input request — assuming the request URL is configured to map to dispatcher servlet
  • Dispatcher Servlet maps the request URL to a handler using handler mapping– in other words, dispatcher servlet maps the request to an execution chain comprising a chain of Handler Interceptors and Controller. SpringUrlHandlerMapping can be used to map different URL patterns to different execution chains. For example, URL pattern /news/* could be mapped to interceptors AuthorizationHandlerInterceptor, RequestValidationHandlerInterceptor and NewsController.
  • View Resolver is used to resolve the view name returned by the controller and the view rendered

Below picture depicts the sequence of steps:

Spring Handler Interceptors
Spring Handler Interceptors

Let’s say that request processing logic for URL patterns /news/* and /premiumnews/* is exactly the same except that /premiumnews/* is accessible only for users with premium subscription. Logic to check if user is signed in and if user has premium subscription can be abstracted in PremiumSubscriptionValidatorHandlerInterceptor and included only in execution chain returned for /premiumnews/*.

<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	<property name="interceptors">
		<list>
			<ref bean="premiumSubscriptionValidatorHandlerInterceptor"></ref>
		</list>
	</property>
	<property name="mappings">
		<props>
			<prop key="/premiumnews/*">newsController</prop>
		</props>
	</property>
</bean>
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	<property name="mappings">
		<props>
			<prop key="/news/*">newsController</prop>
		</props>
	</property>
</bean>

Note that both the URL patterns are mapped to newsController and premiumSubscriptionValidatorHandlerInterceptor is included only in the mapping for /premiumnews/*

Another example: say that there are both web and mobile versions for news content, and web version displays modules “Popular on Facebook” and “News Pulse” — content for these modules are obtained by querying a web service. Since these modules are displayed on web version only, logic to query the web service can be abstracted into an interceptor and included in execution chain returned only for web requests.

Hope you understood how to decouple business logic using handler interceptors!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s