美文网首页
0007.Spring Filter,Interceptor,A

0007.Spring Filter,Interceptor,A

作者: 笑着字太黑 | 来源:发表于2022-04-28 16:22 被阅读0次
1.Filter

无法知道被调用的对象的情报(类,方法,参数)

@Component //也可以通过@Configuration,@Bean进行注册
public class TimeFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("####TimeFilter:time filter init");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("####TimeFilter:time filter start");
         long start = new Date().getTime();
         chain.doFilter(request,response);
        System.out.println("####TimeFilter:time filter"+(new Date().getTime()-start));
        System.out.println("####TimeFilter:time filter finish");
    }

    @Override
    public void destroy() {
        System.out.println("####TimeFilter:time filter destroy");
    }
}
@Configuration
public class WebConfig {
//  正常Filter类有@Component就会被自动注册给Spring了,如果是第三方提供的类,没有@Component,可以用以下方法注册
    @Bean
    public FilterRegistrationBean timeFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        TimerFilter timerFilter = new TimerFilter();
        registrationBean.setFilter(timerFilter);
        List<String> urls = new ArrayList<>();
        urls.add("/*");
        registrationBean.setUrlPatterns(urls);
        return registrationBean;
    }
}
2.Interceptor
@Component//还需要在@Configuration(implements WebMvcConfigurer)类中覆盖addInterceptors方法,注册这个拦截器
public class TimeInterceptor implements HandlerInterceptor {
    @Override//Controller方法调用前执行
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("****TimeInterceptor:preHandle");

        System.out.println("****TimeInterceptor:"+((HandlerMethod)handler).getBean().getClass().getName());
        System.out.println("****TimeInterceptor:"+((HandlerMethod)handler).getMethod().getName());

        //在filter中一个方法完成逻辑的,拦截器中是两个方法 只能用request来存储数据
        request.setAttribute("startTime",new Date().getTime());
        return true;//决定后面是否要执行 Controller中的方法
    }

    @Override//Controller方法处理后调用 Controller方法抛出异常就不会调用了
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("****TimeInterceptor:postHandle");
        long start = (long) request.getAttribute("startTime");
        System.out.println("****TimeInterceptor:time interceptor 耗时:"+(new Date().getTime()-start));
    }

    @Override//不管抛出异常没有 都会执行
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("****TimeInterceptor:afterCompletion");
        System.out.println("****TimeInterceptor:"+((HandlerMethod)handler).getBean().getClass().getName());
        System.out.println("****TimeInterceptor:"+((HandlerMethod)handler).getMethod().getName());
        long start = (long) request.getAttribute("startTime");
        System.out.println("****TimeInterceptor:time interceptor 耗时:"+(new Date().getTime()-start));
        System.out.println("****TimeInterceptor:ex is "+ex);

    }
}
3.Aspect
@Aspect
@Component
public class TimeAspect {
    @Around("execution(* jp..*.controller.*.*(..))")
    public Object handleControllerMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("%%%%TimeAspect:time aspect start");
        String targetName = proceedingJoinPoint.getTarget().getClass().getName();
        System.out.println("%%%%TimeAspect:targetName "+targetName);
        
        Object[] args = proceedingJoinPoint.getArgs();
        for (Object arg : args) {
            System.out.println("%%%%TimeAspect:"+arg.getClass().getName());
            System.out.println("%%%%TimeAspect:arg is " + arg);
        }
        long startTime = new Date().getTime();
        Object obj = proceedingJoinPoint.proceed();
        System.out.println("%%%%TimeAspect:time aspect 耗时" + (new Date().getTime() - startTime));
        System.out.println("%%%%TimeAspect:time aspect end");
        return obj;
    }
}

关于@Around execution

@Around("execution(* jp..*.controller.*.*(..))")
   1.格式为包名.类名.方法名
   2. [..*]:匹配包名中任意多层任意名称的包

相关文章

网友评论

      本文标题:0007.Spring Filter,Interceptor,A

      本文链接:https://www.haomeiwen.com/subject/kbjdyrtx.html