Spring Aop问题汇总

1、Spring中的拦截器 

拦截器的分类:

        HandlerInterceptor:springMVC中的拦截器(handler是方法),拦截的是请求(RequestMappingHandler)

        MethodInterceptor:spring aop 包中的功能,利用AOP机制实现,拦截是spring代理的方法

        AspectJ: apo包增强实现

拦截器的功能:登陆验证、日志耗时打印、权限认证、数据校验、预置自定义数据等

1.1、MethodInterceptor 方法拦截器

a、自定义拦截器,继承 MethodInterceptor 重写 invoke

@Slf4j
@Configuration
public class InjectUserInfoHandler implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Method method = invocation.getMethod();
        XxAnno xxAnno = method.getAnnotation(XxAnno.class);
        if (Objects.nonNull(xxAnno)) {
            //获取参数和参数类型
            Object[] arguments = invocation.getArguments();
            Class[] parameterTypes = method.getParameterTypes();
            //doSomething
        }
        return invocation.proceed();
    }
}

b、使用 Advisor 织入自定义拦截器

//基于JdkRegexpMethodPointcut 自定义包路径
@Bean
public Advisor defaultPointcutAdvisor() {
    JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
    pointcut.setPattern("com.test.*");
    // 配置增强类advisor
    DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
    advisor.setPointcut(pointcut);
    advisor.setAdvice(new InjectUserInfoHandler());
    return advisor;
}

//基于DefaultPointcutAdvisor 自定义注解
@Bean
public Advisor pointcutAdvisor() {
    // 配置增强类advisor
    DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
    //添加自定义注解
    advisor.setPointcut(new AnnotationMatchingPointcut(null, XxAnno.class));
    //注册拦截器
    advisor.setAdvice(new InjectUserInfoHandler());
    return advisor;
}

1.2、HandlerInterceptor 拦截器

SpringMvc中的拦截器,执行优先级高于MethodInterceptor

实现方式:实现HandlerInterceptor接口、继承HandlerInterceptorAdapter类

a、自定义拦截器实现HandlerInterceptor接口

@Slf4j
@Getter
public class CusSsoInterceptor  implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //dSomething
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //dSomething
    }
}

b、注册拦截器

@Slf4j
@Configuration
public class CusInterceptorConfig implements WebMvcConfigurer {
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        WebMvcConfigurer.super.addInterceptors(registry);
        registry.addInterceptor(new CusInterceptor()).addPathPatterns("/**");
    }
}

1.3、基于AspectJ的注解或配置实现拦截功能

自定义注解并启用注解

@Slf4j
@Aspect
@Order(1)
@Component
public class TestAspect {

    @Pointcut("@annotation(AnnotationClassPath)")
    public void aopPoint() {
    }

    @Before("aopPoint()")
    public void writeLog(JoinPoint joinPoint) {
        //doSomething
    }

    @AfterReturning(value = "aopPoint()", returning = "returnObj")
    public void afterReturning(JoinPoint joinPoint, Object returnObj) {
        //doSomething
    }

    @AfterThrowing(value = "aopPoint()", throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint, Exception ex) {
        //doSomething
    }
}

你可能感兴趣的:(Spring,Java,spring,java,后端)