SpringBoot中的Filter,Interceptor,Aspect

Filter java本身自带的过滤器,实现方式

import javax.servlet.*;
import java.io.IOException;

public class TestFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    // 请求前
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

    }
    // 请求后
    @Override
    public void destroy() {

    }
}

Interceptor spring自带的拦截器.

public class TestInterpection implements HandlerInterceptor {

    // 请求控制器接口签名
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return false;
    }
    // controller方法执行结束后,返回视图前
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }
    // 视图渲染后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}

Aspect: spring aop 适用于日志统计

@Aspect
@Component
public class HttpInter {

    // 公用的切面方法地址
    @Pointcut("execution(public * com.imooc.girl.HelloController.*(..))")
    public void log(){

    }
    // 前置
    @Before("log()")
    public void beforeLog(){

    }
    // 后置
    @After("log()")
    public void afterLog(){

    }
}

你可能感兴趣的:(SpringBoot中的Filter,Interceptor,Aspect)