Aspectj @Around pointcut all methods in Java

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

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...

Error: applying to joinpoint that doesn't return void

and here is my 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

回答1:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!