"Spring Combat"--9 AOP Surround Notification

Source: Internet
Author: User
Tags money back

If there is such a scenario, how long does it take to count the time a method executes ?

The typical idea is to record the time before the method executes, and then record it again after the method executes, to get the run time.

With spring AOP, it is impossible to use only the pre-and post-methods, because they cannot share variables. This can be achieved quickly by wrapping notifications.

The wrapping notification class is declared first in the Slice notification class:

     Public voidwatchperformance (Proceedingjoinpoint joinpoint) {Try{System. out. println ("begin!"); LongStart =System.currenttimemillis ();                        Joinpoint.proceed (); LongEnd =System.currenttimemillis (); System. out. println ("end! Performance took"+ (End-start) +"milliseconds"); }Catch(Throwable e) {System. out. println ("eee! We want Our money back!"); }    }

To configure Aop:around in the Bean.xml configuration file, lock the method:

<pointcut-ref= "Performance"  method= "Watchperformance"  />

The results of this implementation are as follows:

The audience is taking their seats. The audience is turning off their cellphonesbegin! Instrumentalist age:25playing jingle Bells:toot toot Tootclap clap clapend!        Performance took milliseconds

So you can see that the process of AOP execution is as follows:

Before () Around () execution method () After/throw () Around ()

"Spring Combat"--9 AOP Surround Notification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.