如何在AspectJ的方面中捕获System.out.println()的参数并在切入点中显示? [英] How to capture the parameters of System.out.println() in AspectJ's aspect and display in pointcut?

查看:52
本文介绍了如何在AspectJ的方面中捕获System.out.println()的参数并在切入点中显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对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的方面中捕获System.out.println()的参数并在切入点中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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