[Done]Spring @Pointcut 切点调用不到(SpringAOP嵌套方法不起作用) 注意事项

本文详细分析了Spring AOP中@Pointcut切点在类内方法相互调用时失效的原因,解释了JDK动态代理的工作原理。当一个方法内部调用被代理的另一个方法时,实际执行的是原始类的逻辑,而非动态代理的逻辑。解决方案包括在调用点使用代理对象或调整切点位置。理解动态代理的实现原理是解决问题的关键。
摘要由CSDN通过智能技术生成

今天在开发过程中,遇到一个问题卡了很久,测试代码如下:

package spring.pointcut;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

/**
 * @Description: Pointcut测试
 * @Author: qionghui.fang
 * @Date: 2018/10/23 上午10:10
 */
@Aspect
public class TargetMonitor {

    // 配置方法1
//    @Pointcut("execution(* spring.pointcut.Target.onEvent(String))")
//    private void anyMethod() {}
//    @Around("anyMethod()")

    //配置方法2
    @Around("execution(* spring.pointcut.Target.onEvent(..))")
    public Object monitor(ProceedingJoinPoint point) throws Throwable {
        System.out.println("before");
        try {
            return point.proceed();
        } finally {
            System.out.println("after");
        }
    }
}

目标类:

public class Target {


    public void otherEvent(){
        System.out.println("Call otherEvent()");
    }


    public boolean onEvent(Integer type, Long Value){
        System.out.println("Call onEvent(Integer type, Long Value)");
        for (int i=0; i<=3; i++){
            onEvent("");
        }
        System.out.println("End Call onEvent(Integer type, Long Value)");
        return true;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值