如何在AspectJ的aspect中捕获System.out.println()的参数并在pointcut中显示? [英] How to capture the parameters of System.out.println() in AspectJ's aspect and display in pointcut?
问题描述
我对 AspectJ 很陌生……刚开始学习.到目前为止,我能够在我的方面获取用户定义方法的参数并在我的切入点中打印捕获的参数.奇怪的是,我开始考虑在切入点的建议中打印 System.out.println()
的内容并编写以下简单代码:
I am very new to AspectJ...just started to learn.
Till now i am able to get the parameters of user defined method in my aspect and print the captured parameter in my pointcut.
Curiously i started to think to print the contents of System.out.println()
in my pointcut's advice and written the following simple code:
HelloClass.java:
HelloClass.java:
public class HelloClass
{
public static void main(String a[])
{
System.out.println("hello sachin");
}
}
HelloAspect.java:
HelloAspect.java:
public aspect HelloAspect
{
pointcut callPointcut(String message ) :call(void java.lang.System.out.println(String))&& args(message);
before( String message) : callPointcut(message )
{
System.out.println("Fetced in point cut:"+message);
//System.out.println("In the advice attached to the call pointcut");
}
}
然后我使用 ajc HelloClass.java HelloAspect.java
编译了这两个文件一穿就编译成功如下:当我使用 java HelloClass
运行程序时它输出为:Hello sachin
它应该是 Fetced in point cut:Hello sachin
.
Then i compiled both the files using ajc HelloClass.java HelloAspect.java
It compiled successfully with one worning as follows:
and when i run program using java HelloClass
it Outputs as: Hello sachin
where it should be as Fetced in point cut:Hello sachin
.
所以任何人都可以指出我哪里出错或遗漏了什么..先感谢您 ..
So can anyone point out where i am going wrong or missing something . .Thank you in advance . .
推荐答案
我仍然感到震惊 .. 发帖 2 天后,没有人回答我的这个问题...但很好,我找到了解决方案并意识到我在哪里错误的.这是我的 println method() 签名中的错误.
I was shocked still .. 2 days after posting,no one answered my this question... but its fine i have found a solution and realized where i was wrong. it was the mistake in my signature of println method().
这是 println 的正确签名:
This is the correct signature of println:
void around(String str) :
call(void java.io.PrintStream.println(String)) && args(str)
对我来说效果很好..
这篇关于如何在AspectJ的aspect中捕获System.out.println()的参数并在pointcut中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!