spring filter、interception、AOP之间对比

spring的filter、interception、aop对比

filter

在spring boot中定义filter实现filter方法。filter方法能够拿到执行的request和response,获取不到具体执行的controller

//@Component
public class TimeFilter implements Filter {

    /* (non-Javadoc)
     * @see javax.servlet.Filter#destroy()
     */
    @Override
    public void destroy() {
        System.out.println("time filter destroy");
    }

    /* (non-Javadoc)
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("time filter start");
        long start = new Date().getTime();
        chain.doFilter(request, response);
        System.out.println("time filter 耗时:"+ (new Date().getTime() - start));
        System.out.println("time filter finish");
    }

    /* (non-Javadoc)
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    @Override
    public void init(FilterConfig arg0) throws ServletException {
        System.out.println("time filter init");
    }

}

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    
    @SuppressWarnings("unused")
    @Autowired
    private TimeInterceptor timeInterceptor;
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//      registry.addInterceptor(timeInterceptor);
    }
    
//  @Bean
    public FilterRegistrationBean timeFilter() {
        
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        
        TimeFilter timeFilter = new TimeFilter();
        registrationBean.setFilter(timeFilter);
        // 指定哪些路径起作用
        List urls = new ArrayList<>();
        urls.add("/*");
        registrationBean.setUrlPatterns(urls);

        
        return registrationBean;
        
    }

}

interception

spring boot中定义interception需要实现HandlerInterceptor,interception能够拿到具体是哪个方法执行的和requset、response。但是拿不到具体方法中的参数。

@Component
public class TimeInterceptor implements HandlerInterceptor {

    /* (non-Javadoc)
     * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        System.out.println("preHandle");
        
        System.out.println(((HandlerMethod)handler).getBean().getClass().getName());
        System.out.println(((HandlerMethod)handler).getMethod().getName());
        
        request.setAttribute("startTime", new Date().getTime());
        //返回的bool值,决定是否后面的方法是否执行
        return true;
    }

    /* (non-Javadoc)
     * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
        Long start = (Long) request.getAttribute("startTime");
        System.out.println("time interceptor 耗时:"+ (new Date().getTime() - start));

    }

    /* (non-Javadoc)
     * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("afterCompletion");
        Long start = (Long) request.getAttribute("startTime");
        System.out.println("time interceptor 耗时:"+ (new Date().getTime() - start));
        System.out.println("ex is "+ex);

    }

}

aop

spring中定义aop需要指定切点,和执行逻辑。能够拿到具体方法的参数。但是获取不到request和response

//@Aspect
//@Component
public class TimeAspect {
    //@Before()
    //@After()

    
    @Around("execution(* com.imooc.web.controller.UserController.*(..))")
    public Object handleControllerMethod(ProceedingJoinPoint pjp) throws Throwable {
        
        System.out.println("time aspect start");
        
        Object[] args = pjp.getArgs();
        for (Object arg : args) {
            System.out.println("arg is "+arg);
        }
        
        long start = new Date().getTime();
        
        Object object = pjp.proceed();
        
        System.out.println("time aspect 耗时:"+ (new Date().getTime() - start));
        
        System.out.println("time aspect end");
        
        return object;
    }

}

总结

filter:filter方法能够拿到执行的request和response,获取不到具体执行的controller
interception:interception能够拿到具体是哪个方法执行的和requset、response。但是拿不到具体方法中的参数。
Aop:能够拿到具体方法的参数,获取不到requset、response
他们之间执行的顺序:filter-> interception->@Around aop->具体执行的方法->产生异常->@Around aop->@ControllerAdvice 全局异常处理-> interception-> filter

你可能感兴趣的:(spring filter、interception、AOP之间对比)