如何通过Spring AOP记录私有方法?

Raj*_*kar 2 aop spring aspectj spring-aop aopalliance

我无法使用spring aop performance logging记录私有方法.以下是我在配置下使用的配置

<aop:config proxy-target-class="true">
        <aop:pointcut id="allServiceMethods" expression="execution(* com.mycom.app.abc..*.*(..))"/>
        <aop:advisor pointcut-ref="allServiceMethods" advice-ref="performanceMonitor" order="2"/>
    </aop:config>
Run Code Online (Sandbox Code Playgroud)

cglib我的班级路径上有罐子.

小智 6

您必须使用编译时编织而不是Spring AOP的代理使用.

来自Spring AOP - 支持的切入点指示符

由于Spring的AOP框架基于代理的特性,受保护的方法根据定义不会被拦截,既不用于JDK代理(这不适用),也不用于CGLIB代理(这在技术上可行,但不建议用于AOP).因此,任何给定的切入点都只能与公共方法匹配!

如果您的拦截需要包括受保护/私有方法甚至构造函数,请考虑使用Spring驱动的本机AspectJ编织而不是Spring的基于代理的AOP框架.这构成了具有不同特征的不同AOP使用模式,因此在做出决定之前一定要先熟悉编织.