Aspectj @Around切入Java中的所有方法 [英] Aspectj @Around pointcut all methods in Java
问题描述
我正在编写一个简单的计时器方面来检测属于我项目的所有包中的所有方法。但是,那些类中各种方法的返回类型是不同的,我得到的是错误:
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:
它只适用于setter但是不适用于getter ...
It only works for setter but not for getter...
错误:应用于不返回void的joinpoint
Error: applying to joinpoint that doesn't return void
这是我的 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.
谢谢
推荐答案
你应该从你的建议中捕获输出按照以下方式从你周围的建议中调用并返回:
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;
}
这将照顾你的所有电话
这篇关于Aspectj @Around切入Java中的所有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!