本文主要是介绍五十、Filter过滤器,Interceptor拦截器,ControllerAdvice,Aspect切片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Filter过滤器,Interceptor拦截器,ControllerAdvice,Aspect切片
上图为在web项目中,在处理request请求时得处理顺序
在项目中我们改怎么使用呢?
Filter过滤器
/*** Created by GAOMINGQIAN on 2017/12/10.*<pr>* Filter只能获取到请求的request和response。获取不到其他的信息* 因为Filter属于J2EE的内容,不知道spring* 会拦截所有的请求*</pr>**/
//@Component
public class TimeFilter implements Filter {//初始化的时候调用@Overridepublic void init(FilterConfig filterConfig) throws ServletException {System.out.println("time filter init");}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println("time filter start");long start=new Date().getTime();filterChain.doFilter(servletRequest,servletResponse);System.out.println("time filter end:"+(new Date().getTime()-start));System.out.println("time filter finsh");}//销毁时调用@Overridepublic void destroy() {System.out.println("time filter destory");}
}
上述内容中有一个@Component注解,当我们不想使用时或者更细的粒度拦截,需要在config类中进行注册
//配置一个过滤器
@Bean
public FilterRegistrationBean timeFilter() {//filter注册用的beanFilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();TimeFilter timeFilter = new TimeFilter();filterRegistrationBean.setFilter(timeFilter);List<String> urls = new ArrayList<>();urls.add("/*");//设置filter对那些请求有作用,这里设置的是对所有的请求都有作用filterRegistrationBean.setUrlPatterns(urls);return filterRegistrationBean;
}
Interceptor拦截器
/*** Created by GAOMINGQIAN on 2017/12/10.*/
public class TimeInterceptor implements HandlerInterceptor {//该方法的返回值决定后续的方法是否执行(controller中的方法)@Overridepublic boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {System.out.println("preHandle");//获取controller类名称System.out.println(((HandlerMethod) o).getBean().getClass().getName());//获取执行method的名字System.out.println(((HandlerMethod)o).getMethod().getName());httpServletRequest.setAttribute("startTime", new Date().getTime());return true;}//当controller抛出异常时,该方法不会被调用@Overridepublic void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {System.out.println("postHandle");long start = (Long) httpServletRequest.getAttribute("startTime");System.out.println("time interceptor耗时" + (new Date().getTime() - start));}@Overridepublic void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {System.out.println("afterCompletion");long start = (Long) httpServletRequest.getAttribute("startTime");System.out.println("time interceptor耗时" + (new Date().getTime() - start));//当无异常的时候,e的值为null,有自定义异常时,也会为NullSystem.out.println(e);}
}
需要在配置类中声明才能使用,继承WebMvcConfigurerAdapter
//告诉spring这是一个配置类
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{@Overridepublic void addInterceptors(InterceptorRegistry registry) {//注册当前的拦截器registry.addInterceptor(new TimeInterceptor());}
}
Aspect切片使用
/*** Created by GAOMINGQIAN on 2017/12/10.*/
@Aspect
@Component
public class TimeAspect {//around中的execution中的内容代表对哪些方法进行拦截@Around("execution(* study.security.web.controller.UserController.*(..))")public Object handlerControllerMethod(ProceedingJoinPoint pjp) throws Throwable {System.out.println("time aspect start");//获取执行方法的参数Object[] args = pjp.getArgs();long start = new Date().getTime();//执行拦截的方法 result为拦截方法的返回值Object result = pjp.proceed();System.out.println("time aspect 耗时:" + (new Date().getTime() - start));return result;}
}
ControllerAdvice,主要用于异常处理的控制器
/*** Created by GAOMINGQIAN on 2017/12/10.*/
@ControllerAdvice
public class ControllerHandlerException {@ExceptionHandler(UserNotExistException.class)@ResponseBody@ResponseStatus(HttpStatus.BAD_REQUEST)public Map<String, Object> handleUserNotExistException(UserNotExistException ex) {Map<String, Object> result = new HashedMap();//放置需要响应的内容result.put("message",ex.getMessage());return result;}
}
优缺点介绍:
Filter过滤器:可以拿到request,response但是拿不到处理方法的信息
Interceptor拦截器:可以拿到request,response,也可以拿到处理方法的信息,但是拿不到处理方法参数的值
Aspect切片:可以拿到处理方法的所有信息,但是拿不到request,response
这篇关于五十、Filter过滤器,Interceptor拦截器,ControllerAdvice,Aspect切片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!