一、通过Filter这个大家很熟悉了吧,这是java规范的一个过滤器,他会拦截请求。在springboot中一般有两种配置方式。这种过滤器拦截并不知道你用的是哪一个Controller处理也不知道你用哪一个方法处理。
过滤器使用的主要是反射 ;拦截器使用的主要是回调 ;AOP使用的主要是动态代理。
一个请求过来 ,先进行过滤器处理,看程序是否受理该请求。过滤器放过后, 程序中的拦截器进行处理,处理完后进入被AOP动态代理重新编译过的主要业务类进行处理 。
Filter:和框架无关,可以控制最初的http请求,但是更细一点的类和方法控制不了。
Interceptor:可以控制请求的控制器和方法,但控制不了请求方法里的参数(用于处理页面提交的请求响应并进行处理,例如做国际化,做主题更换,过滤等)。
Aspect : 可以自定义切入的点,有方法的参数,但是拿不到http请求,可以通过其他方式如RequestContextHolder获得。
Filter,Interceptor,Aspect 实际上都是对Aop的具体实现。都是对业务逻辑的提取。都可以实现权限检查,日志记录。不同的是使用的范围不同,规范不同,深度不同。
一、Filter
通过Filter这个大家很熟悉了吧,这是java规范的一个过滤器,他会拦截请求。在springboot中一般有两种配置方式, 这种过滤器拦截并不知道你用的是哪一个Controller处理也不知道你用哪一个方法处理。
(1)第一种直接写类实现这个接口。代码如下这个要使用Component注解,当你请求服务器的时候他会对每一个请求进行处理。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package com.nbkj.webFilter; import org.springframework.stereotype.Component; import javax.servlet.*; import java.io.IOException; import java.util.Date; @Component public class TimerFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("Time filter init"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("Time filter start"); filterChain.doFilter(servletRequest, servletResponse); System.out.println("Time filter finish"); } @Override public void destroy() { System.out.println("Time filter destroy"); } } |
(2)第二种可以在WebConfig中配置,这种配置方式为了使用第三方的Filter没有@Compont注解所以使用。代码如下
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package com.nbkj.config; import com.nbkj.webFilter.TimerFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; import java.util.List; @Configuration public class WebConfig { @Bean public FilterRegistrationBean timeFilter() { FilterRegistrationBean registrationBean = new FilterRegistrationBean(); TimerFilter timerFilter = new TimerFilter(); registrationBean.setFilter(timerFilter); List urls.add(" @Component public class TimeInterceptor implements HandlerInterceptor {
@Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception { System.out.println("preHandle"); System.out.println(((HandlerMethod) handler).getBean().getClass().getName()); System.out.println(((HandlerMethod) handler).getMethod().getName()); httpServletRequest.setAttribute("startTime", new Date().getTime()); return false; }
@Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle"); Long startTime = (Long) httpServletRequest.getAttribute("startTime"); System.out.println("time interceptor 耗时" + (new Date().getTime() - startTime)); }
@Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("afterCompletion"); Long startTime = (Long) httpServletRequest.getAttribute("startTime"); System.out.println("time interceptor 耗时" + (new Date().getTime() - startTime)); System.out.println("ex is" + e); } } |
添加到拦截器中,WebConfig拦截器注册中心:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package com.nbkj.config; import com.nbkj.interceptor.TimeInterceptor; import com.nbkj.webFilter.TimerFilter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.util.ArrayList; import java.util.List; @Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Autowired private TimeInterceptor timeInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(timeInterceptor); }
} |
三、AOP的基本概念
- Aspect(切面):通常是一个类,里面可以定义切入点和通知
- JointPoint(连接点):程序执行过程中明确的点,一般是方法的调用
- Advice(通知):AOP在特定的切入点上执行的增强处理,有before,after,afterReturning,afterThrowing,around
- Pointcut(切入点):就是带有通知的连接点,在程序中主要体现为书写切入点表达式
- AOP代理:AOP框架创建的对象,代理就是目标对象的加强。Spring中的AOP代理可以使JDK动态代理,也可以是CGLIB代理,前者基于接口,后者基于子类
Spring AOP
Spring中的AOP代理还是离不开Spring的IOC容器,代理的生成,管理及其依赖关系都是由IOC容器负责,Spring默认使用JDK动态代理,在需要代理类而不是代理接口的时候,Spring会自动切换为使用CGLIB代理,不过现在的项目都是面向接口编程,所以JDK动态代理相对来说用的还是多一些。
基于注解的AOP配置方式
启用@AsjectJ支持,在applicationContext.xml中配置下面一句:
- Before:在目标方法被调用之前做增强处理,@Before只需要指定切入点表达式即可
- AfterReturning:在目标方法正常完成后做增强,@AfterReturning除了指定切入点表达式后,还可以指定一个返回值形参名returning,代表目标方法的返回值
- AfterThrowing:主要用来处理程序中未处理的异常,@AfterThrowing除了指定切入点表达式后,还可以指定一个throwing的返回值形参名,可以通过该形参名
来访问目标方法中所抛出的异常对象
- After:在目标方法完成之后做增强,无论目标方法时候成功完成。@After可以指定一个切入点表达式
- Around:环绕通知,在目标方法完成前后做增强处理,环绕通知是最重要的通知类型,像事务,日志等都是环绕通知,注意编程中核心是一个ProceedingJoinPoint,使用环绕通知,切入要切入的类,当请求的时候回拦截下来,这样可以获取拦截的方法的参数。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.nbkj.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import java.util.Date; @Aspect @Component public class TimeAspect { @Around("execution(* com.nbkj.controller.UserController.*(..))") public Object handleControllerMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("time aspect start"); Object[] args = proceedingJoinPoint.getArgs(); for (Object arg : args) { System.out.println(arg.getClass().getName()); System.out.println("arg is " + arg); } long startTime = new Date().getTime(); Object obj = proceedingJoinPoint.proceed(); System.out.println("time aspect 耗时" + (new Date().getTime() - startTime)); System.out.println("time aspect end"); return obj; } } |
过滤器(Filter) :可以拿到原始的http请求,但是拿不到你请求的控制器和请求控制器中的方法的信息。
拦截器(Interceptor):可以拿到你请求的控制器和方法,却拿不到请求方法的参数。
切片 (Aspect) : 可以拿到方法的参数,但是却拿不到http请求和响应的对象。
Filter,Interceptor和SpringAop - 爱跑步的星仔 - 博客园
拦截器和过滤器的区别:
拦截器是基于java反射机制的,过滤器是基于函数回调。
拦截器不依赖于selvet容器,过滤器依赖于servlet容器(使用过滤器,我们需要从spring容器当中取出Bean对象)。
拦截器只能对action请求起作用,过滤器几乎可以对所有请求起作用。
在action生命周期中拦截器可以多次调用,过滤器只能在初始化的时候使用一次



