• Spring 嵌套方法AOP不生效问题


    问题描述, 如下Abc定义为一个Bean, b()方法添加@TargetDatasource,定义切面DynamicDataSourceAspect,期望:调用a()方法,b()方法上的AOP拦截能生效。实际不生效。

    @Service
    
    public class Abc {
    
    public void a(){
    b();
    }

    @TargetDatasource(value=abc) public void b(){
    } }

    AOP代码:

    @Order(-1)
    @Component
    @Slf4j
    @Aspect
    public class DynamicDataSourceAspect {
    
      /**
       * set data source value.
       */
      @Before(value = "@annotation(targetDataSource)", argNames = "targetDataSource")
      public void setDataSource(TargetDataSource targetDataSource) {
        if (targetDataSource != null && targetDataSource.value() != null) {
          log.info("Databse value: {}", targetDataSource.value());
          DynamicDataSourceHolder.set(targetDataSource.value());
        }
      }
    
      /**
       * clear data source value.
       */
      @After(value = "@annotation(targetDataSource)", argNames = "targetDataSource")
      public void clearDataSource(TargetDataSource targetDataSource) {
        DynamicDataSourceHolder.clear();
      }
    }

    问题分析:

    我们都知道Spring aop有两种实现方式,基于Interface生成代理和cglib生成代理。上面的例子中,当调用a()方法时,调用者拿到的是Abc的代理类,即增强类。如果a()方法上有@TargetDatasource注解,拦截会生效。然而,在a()方法里调用b(),b方法上的拦截不会生效。原因是因为a()调用b(), 用的是b()方法的目标类,而不是代理类,所以拦截不生效。

    解决方法:

    1. 重构代码, 把b()方法移到一个Bean里面。

    2. 调整a()方法如下:

    @Service
    
    public class Abc {
    
    public void a(){
       ((Abc)AopContext.currentProxy()).b();
    }
    
    @TargetDatasource(value=abc)
    public void b(){
    }
    }

    Sping 官方解释 - https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#aop 

  • 相关阅读:
    ab测试curl json语句
    Python进阶-----静态方法@property,@classmethod,@staticmethod【转】
    staticmethod写和不写有什么区别?【转】
    使用 febootstrap 制作自定义基础镜像【转】
    k8s
    Linux服务器扩容VG时报错 Couldn't create temporary archive name.
    2020年的一些思考和总结
    谈谈FTP
    NET::ERR_INCOMPLETE_CHUNKED_ENCODING 200 (OK)
    Centos7 cache/buff过高处理方法
  • 原文地址:https://www.cnblogs.com/lzmrex/p/9450911.html
Copyright © 2020-2023  润新知