Spring [combat] - 9 AOP around advice

If there is such a scenario, we need to count the time a method of execution , how to do it?

  Typical think of recording time before execution method, recording method performs again, come running time.

 

If the Spring AOP, using only pre- and post method can not be done, because they can not be shared variable. Surrounded by this notice, you can quickly achieve.

  First, notice the class declaration section around advice classes:

public void watchPerformance(ProceedingJoinPoint joinpoint){
        try{
            System.out.println("begin!");
            long start = System.currentTimeMillis();
            
            joinpoint.proceed();
            
            long end = System.currentTimeMillis();
            System.out.println("end!        performance took "+(end-start)+" milliseconds");
        }catch(Throwable e){
            System.out.println("eee!We want our money back!");
        }
    }

  Configuration bean.xml profile aop: around, locking methods:

<aop:around pointcut-ref="performance" method="watchPerformance"/>

  The results of such execution are as follows:

The audience is taking their seats.
The audience is turning off their cellphones
begin!
Instrumentalist age:25
Playing Jingle Bells:TOOT TOOT TOOT
CLAP CLAP CLAP
end!        performance took 95 milliseconds

  Thus it can be seen AOP process performed as follows:

  before()
  around()
  执行方法()
  after/throw()
  around()

Reproduced in: https: //my.oschina.net/u/204616/blog/545363

Guess you like

Origin blog.csdn.net/weixin_34194702/article/details/91989881