过滤器、拦截器、AOP、异常处理器是搭建系统框架时,经常用到的部分,异常处理器的作用很明显,就是处理接口执行过程中的异常,而过滤器、拦截器和AOP的作用就很丰富了,日志记录、性能监控、安全认证等等可以向上抽取的功能组件,均可以用他们来实现。
使用@WebFilter注解,创建过滤器,代码如下:
package com.example.demo.filter;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(filterName = "demoFilter",urlPatterns = "/demo/hello")
@Component
public class DemoFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("过滤器创建");
}
@Override
public void destroy() {
System.out.println("过滤器销毁");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
try {
System.out.println("过滤请求开始");
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("过滤请求结束");
}catch (Exception e){
System.out.println("过滤器捕获异常");
throw e;
}
}
}
实现HandlerInterceptor接口,创建拦截器,代码如下:
package com.example.demo.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DemoInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截请求--进入Controller前");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("拦截请求--Controller中方法执行完");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
if (ex != null){
System.out.println("拦截器判断存在异常,但不能处理异常");
}
System.out.println("拦截请求--视图渲染完成后");
}
}
继承WebMvcConfigurationSupport类,配置拦截器,代码如下:
package com.example.demo.interceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class DemoInterceptorConfig extends WebMvcConfigurationSupport {
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new DemoInterceptor()).addPathPatterns("/demo/hello");
super.addInterceptors(registry);
}
}
使用@Aspect注解,创建AOP切面类,代码如下:
package com.example.demo.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* AOP切面
* @author Neo
*/
@Aspect
@Component
public class DemoAop {
/**
* 切点
*/
@Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)")
public void getMapping() {}
/**
* 环绕通知
* @param pjp
* @return
* @throws Throwable
*/
@Around("getMapping()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
try {
System.out.println("切面请求开始");
Object object = pjp.proceed();
System.out.println("切面请求结束");
return object;
}catch (Exception e){
System.out.println("切面捕获到异常");
throw e;
}
}
}
使用@RestControllerAdvice注解,创建异常处理器,代码如下:
package com.example.demo.exception;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class DemoException {
@ExceptionHandler
public Object demoException(Exception e) throws Exception{
System.out.println("全局异常处理器捕获到异常");
throw e;
}
}
代码如下:
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo")
public class DemoController {
@GetMapping(value = "/hello")
public String hello(){
System.out.println("执行方法");
// int a = 1/0;
return "hello world!";
}
}