Spring AOP 方法内部调用不生效

问题描述

最近有个需求,统计某个方法的调用次数,开始使用 Spring AOP 实现,后来发现当方法被内部调用时,切面逻辑将不会生效,直接上样例:

定义接口,包含方法 A,方法 B

public interface ISon {
    void A();
    void B();
}

定义接口实现,方法 B 调用方法 A

@Service
public class Son implements ISon {
    @Override
    public void A() {
        System.out.println("method A");
    }
    @Override
    public void B() {
        System.out.println("method B");
        A();
    }
}

切点定义,对方法 A 进行增强

@Aspect
@Component
public class AspectDemo {

    @Pointcut(value = "execution(* com.example.transactiondemo.aop.ISon.A(..))")
    public void pointCut(){
    }

    @Before("pointCut()")
    public void before(JoinPoint joinPoint){
        System.out.println("before ");
    }

    @After("pointCut()")
    public void after(){
        System.out.println("after ");
    }

}

测试

@Autowired
private ISon iSon;

@Test
public void test11(){
    iSon.A();
    System.out.println("-----------");
    iSon.B();
}

打印输出

before 
method A
after 
-----------
method B
method A

可以发现,如果直接使用 实例调用 A,会进行增强;但是如果是方法 B 调用,A 的增强逻辑将失效。

失效原因

方法 A 被调用,是基于 AOP 生成的 代理对象 进行的调用;方法 B 调用方法 A ,是 this 目标对象 直接调用,并不是代理对象进行调用

Spring AOP

5.8. Proxying Mechanisms

解决方案

回到最开始的需求,如何通过切面的方式获取某一个方法的调用次数? 包括方法自调用

  1. 使用 AopContext.currentProxy()

    切面开启注解

    @EnableAspectJAutoProxy(exposeProxy = true,proxyTargetClass = true)
    

    方法 B 不直接调用 A

        @Override
        public void B() {
            System.out.println("method B");
            ((Son) AopContext.currentProxy()).A();
        }
    
  2. 先获取上下文的 Bean 再调用

    @Autowired
    private ApplicationContext applicationContext;
    
    ((Son) applicationContext.getBean(Son.class)).A();
    

参考

Spring AOP:内部调用陷阱

  • 9
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Spring AOP生效的原因可能有以下几种情况。 首先,如果要增强的类或方法没有被Spring管理,那么AOP就无法生效。这种情况下,需要将目标类或方法注入到Spring容器才能实现AOP的增强效果。 其次,如果要被增强的类或方法同时被SpringSpring MVC扫描,可能会导致AOP失效。这是因为在Spring MVC容器重新实例化的对象并没有被AOP代理,而是由Spring MVC容器直接管理。为了解决这个问题,我们需要确保AOP配置的切点表达式声明与要切入的类和方法在同一个容器。 另外,如果要增强的方法被同类的方法调用,也可能导致AOP不生效。这是因为AOP是在目标方法调用时才会生效,而同类的方法调用不会触发AOP。要解决这个问题,可以将目标方法抽取为单独的方法,然后在原来的方法和其他使用该方法的地方进行调用。 还有一种情况是,如果使用了注解方式进行AOP,但没有开启注解支持,也会导致AOP不生效。要解决这个问题,需要在Spring配置文件添加注解支持的配置。 综上所述,要解决Spring AOP不生效的问题,需要注意以上几种情况,并根据具体情况进行相应的修改和调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Spring Aop失效的情況及解决办法](https://blog.csdn.net/weixin_43790613/article/details/113091496)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值