如何通过 Spring AOP 记录私有方法? [英] How can I log private methods via Spring AOP?

查看:42
本文介绍了如何通过 Spring AOP 记录私有方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用 spring aop 性能日志记录私有方法.以下是我在以下配置中使用的配置

I am not able to log the private methods using spring aop performance logging. Below is the configuration I am using below configuration

<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>

我的类路径上有 cglib jar.

I am having cglib jar on my class path.

推荐答案

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

You have to use compile time weaving instead of the proxy usage for Spring AOP.

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

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

Due to the proxy-based nature of Spring’s AOP framework, protected methods are by definition not intercepted, neither for JDK proxies (where this isn’t applicable) nor for CGLIB proxies (where this is technically possible but not recommendable for AOP purposes). As a consequence, any given pointcut will be matched against public methods only!

如果你的拦截需要包含protected/private方法甚至构造函数,可以考虑使用Spring驱动的原生AspectJ织入,而不是Spring的基于代理的AOP框架.这构成了不同特点的不同AOP使用模式,所以一定要先熟悉织法再做决定.

If your interception needs include protected/private methods or even constructors, consider the use of Spring-driven native AspectJ weaving instead of Spring’s proxy-based AOP framework. This constitutes a different mode of AOP usage with different characteristics, so be sure to make yourself familiar with weaving first before making a decision.

这篇关于如何通过 Spring AOP 记录私有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆