Spring Learning Notes (17)--Return notification & exception notification & Surround Notification

Source: Internet
Author: User
Tags aop

I followed the Spring Learning Note (--AOP) to return notification before

    /**
     * Return Notification: The method is called after normal execution and, if there is an exception, the return value
     of the *             can access to the method is not invoked *
     @param joinpoint
     * @param result   method
     *
    /@AfterReturning (value= "Execution (* com.zj.asceptj.*.* (..))", returning= "result") public
    Void Afterreturning (joinpoint joinpoint,object result) {
        //method name
        String methodname = Joinpoint.getsignature (). GetName ();
        System.out.println ("Method" +methodname+ "End:" +result);
    }
Exception Notification
    /**
     * Exception notification:
     *            You can specify which exceptions occur when the method is abnormal: how to specify. specified by an entry parameter, such as:
     * If the join           parameter is Nullpointexception ex, the 
     * @param joinpoint
     * @param ex
    * * is only performed when a null pointer exception occurs Afterthrowing (value= "Execution (* com.zj.asceptj.*.* (..))", throwing= "ex") public
    void Afterthrowing (joinpoint Joinpoint,exception ex) {
        //method name
        String methodname = Joinpoint.getsignature (). GetName ();
        System.out.println ("method" +methodname+ "occurs:" +ex);
    }
Surround NotificationSurround notification is similar to the whole process of dynamic agents, you can use surround notification to implement the forward notification, post notification, return notification, exception notification function, very powerful, but not commonly used on dynamic proxies, see Spring Learning Notes (--AOP) basics
/** * Surround notification needs to carry a proceedingjoinpoint type of parameter * wrapping notification is similar to the whole process of dynamic proxies: Proceedingjoinpoint This type of parameter Number can determine whether to execute the target method * and the surround notification must have a return value, which is the return value of the target method * @param PJD/@Around ("Execution (* com.zj.asceptj.*.* (..))
        ") public Object Around (Proceedingjoinpoint pjd) {object result = NULL;

        String methodname = Pjd.getsignature (). GetName (); try {//forward notification System.out.println (' method: ' +methodname+ ' begins with ' +arrays.aslist (Pjd.getargs ())
            ;
            Execute target method result = Pjd.proceed ();
        Returns the notification System.out.println ("method:" +methodname+ "End With" +result);
        catch (Throwable e) {//Exception notification System.out.println ("method:" +methodname+ "occurs exception" +e);
        //Post Notification System.out.println ("method ends");
    return result; }

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.