Aspectj @Around 切入点 Java 中的所有方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11270459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:28:10  来源:igfitidea点击:

Aspectj @Around pointcut all methods in Java

javaaop

提问by rb8680

i am writing a simple timer aspect to instrument all the methods in all the packages that belong my project. But, then the return types of various methods in those classes are different and I am getting this following error:

我正在编写一个简单的计时器方面来检测属于我的项目的所有包中的所有方法。但是,这些类中各种方法的返回类型是不同的,我收到以下错误

It only works for setter but not for getter...

它只适用于 setter 而不适用于 getter...

Error: applying to joinpoint that doesn't return void

错误:应用于不返回 void 的连接点

and here is my timeraspect...

这是我的timeraspect...

@Around("execution(* com.myproject..*(..))")
public void log(ProceedingJoinPoint pjp) throws Throwable{


    LOG.info("TimerAspect");
    String name = pjp.getSignature().getName();
    Monitor mon = MonitorFactory.start(name);
    pjp.proceed();
    mon.stop();

    LOG.info("TimerAspect Mon" + mon);

    String printStr = mon.getLabel()+","+mon.getUnits()+","+mon.getLastValue()+","+mon.getHits()+","+mon.getAvg()+","+mon.getTotal()+","+mon.getMin()+","+mon.getMax()+","+mon.getFirstAccess()+","+mon.getLastAccess();

    File f = new File("target/stats.csv");
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(f, true));
    bufferedWriter.write(printStr);
    bufferedWriter.newLine();
    bufferedWriter.flush();
    bufferedWriter.close();


}

Any clue to resolve this is greatly appreciated.

非常感谢任何解决此问题的线索。

Thanks

谢谢

回答by Biju Kunjummen

You should capture the output from your adviced call and return that from your around advice along these lines:

您应该从您的建议调用中捕获输出,并按照以下几行从您的周围建议中返回该输出:

@Around("execution(* com.myproject..*(..))")
public Object log(ProceedingJoinPoint pjp) throws Throwable{

....
Object result = pjp.proceed();
......
return result;
}

This will take care of all your calls

这将处理您的所有电话