Spring 3.0

562 views
499 views

Published on

spring 3.0 by Craig Walls

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
562
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Spring 3.0

  1. 1. Spring Interceptor Tutorial06.15.2012 EmailinShare0Views: 1941 Tweet 0 inShareSpring Interceptors has the ability to pre-handle and post-handle the web requests. Eachinterceptor class should extend the HandlerInterceptorAdapter class. Here we will create aLogger Interceptor by extending the HandlerInterceptorAdapter class. You can override anyof the three callback methods preHandle(), postHandle() and afterCompletion(). As thenames indicate the preHandle() method will be called before handling the request, thepostHandle() method will be called after handling the request and the afterCompletion()method will be called after rendering the view.In each method we will log information using log4j. First instantiate the logger in the staticcontext, then set up the basic configuration so that the log messages will be logged on theconsole.The LoggerInterceptor class is shown below.package com.vaannila.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.log4j.BasicConfigurator;import org.apache.log4j.Logger;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.handler. HandlerInterceptorAdapter;public class LoggerInterceptor extends HandlerInterceptorAdapter { static Logger logger = Logger.getLogger(LoggerInterceptor.class); static{ BasicConfigurator.configure(); } @Override public boolean preHandle(HttpServletRequest request,
  2. 2. HttpServletResponse response, Object handler) throwsException { logger.info("Before handling the request"); return super.preHandle(request, response, handler); } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { logger.info("After handling the request"); super.postHandle(request, response, handler, modelAndView); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,Exception ex) throws Exception { logger.info("After rendering the view"); super.afterCompletion(request, response, handler, ex); }} Now the logger interceptor is created you need to associate this interceptor with the handlermapping. Here we use BeanNameUrlHandlerMapping, incase you are using more than onehandler mapping you need to associate the interceptor with each one of them. The code belowshows how to associate an interceptor with the handler mapping.<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> <bean id="handlerMapping"class="org.springframework.web.servlet.handler. BeanNameUrlHandlerMapping"p:interceptors-ref="loggerInterceptor" /> <bean id="loggerInterceptor"class="com.vaannila.interceptor.LoggerInterceptor" /> <bean id="userService" class="com.vaannila.service.UserServiceImpl" /> <bean name="/userRegistration.htm"class="com.vaannila.web.UserController" p:userService-ref="userService"p:formView="userForm" p:successView="userSuccess" /></beans>When you execute the example you can see the log messages getting dispalyed on theconsole.You can download and try the example here.
  3. 3. Spring Interceptor Using AnnotationTutorial06.15.2012 EmailViews: 701If you are using annotated Spring controllers, the only change that you need to do to theprevious interceptor example is the configuration. In the Spring bean configuration fileinstead of using BeanNameUrlHandlerMapping or any other handler mapping useDefaultAnnotationHandlerMapping. The configuration file is shown below.view sourceprint?01.<?xml version="1.0" encoding="UTF-8"?>02.<beans xmlns="http://www.springframework.org/schema/beans"03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"04. xmlns:context="http://www.springframework.org/schema/context"05. xsi:schemaLocation="http://www.springframework.org/schema/beans ;06. http://www.springframework.org/schema/beans/spring-beans.xsd07. http://www.springframework.org/schema/context ;08. http://www.springframework.org/schema/context/spring-context.xsd">09.
  4. 4. 10. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />11.12. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:interceptors-ref="loggerInterceptor" />13.14. <context:component-scan base-package="com.vaannila.web" />15.16. <bean id="loggerInterceptor"class="com.vaannila.interceptor.LoggerInterceptor" />17.18. <bean id="userService" class="com.vaannila.service.UserServiceImpl"/>19.20.</beans>

×