AspectJ 切入点 - 获取对连接点类和名称的引用

发布于 2024-10-18 10:41:19 字数 783 浏览 4 评论 0原文

我使用 @AspectJ 样式来编写方面,以处理我们应用程序中的日志记录。基本上我有一个像这样设置的切入点:

@Pointcut("call(public * com.example..*(..))")
public void logging() {}

然后是像这样的前后建议:

@Before("logging()")
public void entering() {...}
...
@After("logging()")
public void exiting() {...}

我想以以下格式在这些方法中创建日志:

logger.trace("ENTERING/EXITING [" className + "." + methodName "()]");

问题是我不知道如何获取对该类的引用和方法名称。我已经尝试过:

joinPoint.getThis().getClass()

但这似乎返回了调用者的类名。

class A {
    public void a() {
        B.b();
    }
}


class B {
    public void b() {
        ...
    }
}

将导致以下日志

ENTERING [A.b()]

有人可以提供一些有关如何获取实际连接点类和方法名称的帮助吗

I am using the @AspectJ style for writing aspects, to handle logging in our application. Basically I have a pointcut set up like so:

@Pointcut("call(public * com.example..*(..))")
public void logging() {}

and then a before and after advice like so:

@Before("logging()")
public void entering() {...}
...
@After("logging()")
public void exiting() {...}

I want to create a log in these methods in the following format:

logger.trace("ENTERING/EXITING [" className + "." + methodName "()]");

The problem is I don't know how to get a reference to the class and method names. I have tried:

joinPoint.getThis().getClass()

but this seems to return the caller's class name.

class A {
    public void a() {
        B.b();
    }
}


class B {
    public void b() {
        ...
    }
}

would result in the following log

ENTERING [A.b()]

can someone give some help on how to get the actual joinpoint class and method name

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千と千尋 2024-10-25 10:41:20

您需要使用joinPoint.getTarget().getClass()。由于您正在使用建议呼叫连接点,因此您感兴趣的对象就是呼叫的目标。

请注意 API 规范状态

返回目标对象。这将始终与目标切入点指示符匹配的对象相同。除非您特别需要这种反射访问,否则您应该使用目标切入点指示符来获取此对象,以获得更好的静态类型和性能。

没有目标对象时返回 null。

盲目使用joinPoint.getTarget().getClass()可能会导致NullPointerException。考虑使用连接点的签名,例如:

final Signature signature = joinPoint.getSignature();

然后:

final Class clazz = signature.getDeclaringType();

或者,如果您需要的只是类名:

final String clazz = signature.getDeclaringTypeName();

You need to use joinPoint.getTarget().getClass(). Since you are using advising a call join point, the object of your interest is the target of the call.

Please note the API specification states:

Returns the target object. This will always be the same object as that matched by the target pointcut designator. Unless you specifically need this reflective access, you should use the target pointcut designator to get at this object for better static typing and performance.

Returns null when there is no target object.

Blindly using joinPoint.getTarget().getClass() can result in a NullPointerException. Consider using the join point's signature, such as:

final Signature signature = joinPoint.getSignature();

Then:

final Class clazz = signature.getDeclaringType();

Or if all you need is the class name:

final String clazz = signature.getDeclaringTypeName();
傲性难收 2024-10-25 10:41:20
    public void logBefore(JoinPoint joinPoint) {
    logger.info("###### Requested class : {} ; Method : {} ", joinPoint.getTarget().getClass().getName(), joinPoint.getSignature().getName());
    Object[] signatureArgs = joinPoint.getArgs();
    for (Object signatureArg : signatureArgs) {
        logger.info("###### Arguments: {} ", signatureArg.toString());
    }
} 

可能会帮助某人:使用上面的代码来获取请求的类、方法和参数。

    public void logBefore(JoinPoint joinPoint) {
    logger.info("###### Requested class : {} ; Method : {} ", joinPoint.getTarget().getClass().getName(), joinPoint.getSignature().getName());
    Object[] signatureArgs = joinPoint.getArgs();
    for (Object signatureArg : signatureArgs) {
        logger.info("###### Arguments: {} ", signatureArg.toString());
    }
} 

may help someone: Use above code to get the requested Class, method and args.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文