• Spring Aop问题汇总


    1、Spring中的拦截器 

    拦截器的分类:

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

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

            AspectJ: apo包增强实现

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

    1.1、MethodInterceptor 方法拦截器

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

    1. @Slf4j
    2. @Configuration
    3. public class InjectUserInfoHandler implements MethodInterceptor {
    4. @Override
    5. public Object invoke(MethodInvocation invocation) throws Throwable {
    6. Method method = invocation.getMethod();
    7. XxAnno xxAnno = method.getAnnotation(XxAnno.class);
    8. if (Objects.nonNull(xxAnno)) {
    9. //获取参数和参数类型
    10. Object[] arguments = invocation.getArguments();
    11. Class[] parameterTypes = method.getParameterTypes();
    12. //doSomething
    13. }
    14. return invocation.proceed();
    15. }
    16. }

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

    1. //基于JdkRegexpMethodPointcut 自定义包路径
    2. @Bean
    3. public Advisor defaultPointcutAdvisor() {
    4. JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
    5. pointcut.setPattern("com.test.*");
    6. // 配置增强类advisor
    7. DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
    8. advisor.setPointcut(pointcut);
    9. advisor.setAdvice(new InjectUserInfoHandler());
    10. return advisor;
    11. }
    12. //基于DefaultPointcutAdvisor 自定义注解
    13. @Bean
    14. public Advisor pointcutAdvisor() {
    15. // 配置增强类advisor
    16. DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
    17. //添加自定义注解
    18. advisor.setPointcut(new AnnotationMatchingPointcut(null, XxAnno.class));
    19. //注册拦截器
    20. advisor.setAdvice(new InjectUserInfoHandler());
    21. return advisor;
    22. }

    1.2、HandlerInterceptor 拦截器

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

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

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

    1. @Slf4j
    2. @Getter
    3. public class CusSsoInterceptor implements HandlerInterceptor {
    4. @Override
    5. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    6. //dSomething
    7. }
    8. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    9. //dSomething
    10. }
    11. }

    b、注册拦截器

    1. @Slf4j
    2. @Configuration
    3. public class CusInterceptorConfig implements WebMvcConfigurer {
    4. @Override
    5. public void addInterceptors(InterceptorRegistry registry) {
    6. WebMvcConfigurer.super.addInterceptors(registry);
    7. registry.addInterceptor(new CusInterceptor()).addPathPatterns("/**");
    8. }
    9. }

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

    自定义注解并启用注解

    1. @Slf4j
    2. @Aspect
    3. @Order(1)
    4. @Component
    5. public class TestAspect {
    6. @Pointcut("@annotation(AnnotationClassPath)")
    7. public void aopPoint() {
    8. }
    9. @Before("aopPoint()")
    10. public void writeLog(JoinPoint joinPoint) {
    11. //doSomething
    12. }
    13. @AfterReturning(value = "aopPoint()", returning = "returnObj")
    14. public void afterReturning(JoinPoint joinPoint, Object returnObj) {
    15. //doSomething
    16. }
    17. @AfterThrowing(value = "aopPoint()", throwing = "ex")
    18. public void afterThrowing(JoinPoint joinPoint, Exception ex) {
    19. //doSomething
    20. }
    21. }
  • 相关阅读:
    NativeBuffering,一种高性能、零内存分配的序列化解决方案[性能测试篇]
    【bugfix】解决Redis缓存键清理问题
    CSS 四中方法实现水平 垂直居中
    Java 对于文件的操作
    【C/C++笔试练习】初始化列表、构造函数、析构函数、两种排序方法、求最小公倍数
    1028 List Sorting
    Spring学习笔记3 Spring对IOC的实现
    ProGuard使用简介
    Java面试之Redis篇(offer 拿来吧你)
    物联网ARM开发-3协议-单总线应用红外遥控
  • 原文地址:https://blog.csdn.net/qq_27789551/article/details/126078467