Java cglib dynamic agent

cglib dynamic proxy, why does a () call the b (), of the current class this b () is the b (), of the parent class and not the b () of the subclass generated by cglib?

@Component
public class MyTest {
    public void a() {
        System.out.println("it"s a");
        b();
    }
    public void b() {
        System.out.println("it"s b");
    }
}

@Aspect
@Component
@Slf4j
@Getter
public class AspectLog {
    @Pointcut("execution(public * com.vae1970.demo.aspect.MyTest.*(..))")
    public void pointcut() {
    }
    
    @Before("pointcut()")
    public void before(JoinPoint jp) {
        Method method = (MethodSignature) jp.getSignature().getMethod();
        System.out.println("log: function " + method.getName());
    }
}

@RestController
public class TestController {
    @Autowired
    private MyTest myTest;

    @GetMapping("/aspect")
    public String aspect() {
        myTest.a();
        return "ok";
    }
}

expected output

log: function a
it"s a
log: function b
it"s b

actual output

log: function a
it"s a
it"s b

I looked at the MyTest classes generated by cglib, both, a () and b () are proxies, so why didn"t the b () of the proxy classes be executed when actually executed?

Aug.29,2021

because the proxy class actually calls the original method.
is just a proxy class generated by a custom method added before and after the original method.
using cglib in spring, you can see the intercept () method in the org.springframework.aop.framework.CglibAopProxy.DynamicAdvisedInterceptor class, and the final execution of your a () method is the following


MyTestMyTestb

@Component
public class MyTest {
    @Autowired
    private MyTest myTest;
    public void a() {
        System.out.println("it's a");
        myTest.b();
    }
    public void b() {
        System.out.println("it's b");
    }
}
Menu