AspectJ 切入点 - 获取对连接点类和名称的引用
我使用 @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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用
joinPoint.getTarget().getClass()
。由于您正在使用建议呼叫连接点,因此您感兴趣的对象就是呼叫的目标。请注意 API 规范状态:
盲目使用
joinPoint.getTarget().getClass()
可能会导致NullPointerException
。考虑使用连接点的签名,例如:然后:
或者,如果您需要的只是类名:
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:
Blindly using
joinPoint.getTarget().getClass()
can result in aNullPointerException
. Consider using the join point's signature, such as:Then:
Or if all you need is the class name:
可能会帮助某人:使用上面的代码来获取请求的类、方法和参数。
may help someone: Use above code to get the requested Class, method and args.