Spring拦截器和SpringAop实现
一、拦截器
1、aop是面向切面编程,原理是java的发射技术。
2、分为三类,before、after、arround
3、springMvc为我们提供了一个适配器HandlerInterceptorAdapter,继承它实现我们自己的拦截器
4、HandlerInterceptorAdapter例子:
package com.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class BookInterceptor extends HandlerInterceptorAdapter {
private static Logger Logger = LoggerFactory.getLogger(BookInterceptor.class);
@Override
public void afterCompletion(HttpServletRequest paramHttpServletRequest,HttpServletResponse paramHttpServletResponse, Object paramObject, Exception paramException)throws Exception {
}
@Override
public void postHandle(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse,Object paramObject, ModelAndView paramModelAndView) throws Exception {
String url = paramHttpServletRequest.getRequestURI();
Logger.info("postMethod:" + url);
}
@Override
public boolean preHandle(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse,Object paramObject) throws Exception {
String url = paramHttpServletRequest.getRequestURI();
Logger.info("preMehtod:" + url);
return true;
}
}
配置文件写在applicationContext.xml里面
<!--配置拦截器, 多个拦截器,顺序执行 -->
<mvc:interceptors>
<mvc:interceptor><!-- 匹配的是url路径, 如果不配置或/**,将拦截所有的Controller -->
<mvc:mapping path="/**" />
<bean class="com.interceptor.BookInterceptor"></bean>
</mvc:interceptor><!-- 当设置多个拦截器时,先按顺序调用preHandle方法,然后逆序调用每个拦截器的postHandle和afterCompletion方法 -->
</mvc:interceptors>
二、完整的切点切面
1、@aspect例子(同时定义切面和切点)
package com.interceptor;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class BookAspect {
private static final Logger logger = LoggerFactory.getLogger(BookAspect.class);
@Pointcut("execution(* *.*List(..))")
public void bookaspeck() {
}
@Before("bookaspeck()")
public void bookbefore() {
logger.info("before...");
}
@After("bookaspeck()")
public void bookafter() {
logger.info("after...");
}
}
在applicationContext.xml中配置
<aop:aspectj-autoproxy/>
三、只有切面
1、MethodBeforeAdvice,AfterReturningAdvice只写切面,在配置文件里面定义切点
package com.interceptor;
import java.lang.reflect.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
public class BookBeforeAdvice implements MethodBeforeAdvice,AfterReturningAdvice {
private static Logger Logger = LoggerFactory.getLogger(BookBeforeAdvice.class);
public void before(Method paramMethod, Object[] paramArrayOfObject, Object paramObject) throws Throwable {
Logger.info("before...");
}
public void afterReturning(Object paramObject1, Method paramMethod, Object[] paramArrayOfObject,Object paramObject2) throws Throwable {
Logger.info("after...");
}
}
配置文件:
<bean id="bookBefore" class="com.interceptor.BookBeforeAdvice"/>
<bean id="regexpMethodPointcutAdvisor1" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="bookBefore"/>
<property name="patterns" value=".*BookList">
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
或
<bean id="bookBefore" class="com.interceptor.BookBeforeAdvice"/>
<bean id="regexpMethodPointcutAdvisor1" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="bookBefore"/>
<property name="patterns">
<list>
<value>com.book.control.TBookController.queryBookList</value>
</list>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>