[Original]java Web learning Note 106:spring Learn---AOP notifications: pre-notification, post-notification, return notification, exception notification, surround notification

Source: Internet
Author: User

1. Notification Classification:

@before: pre-notification, executed before method execution

@after: Post notification, executed after method execution

@afterrunning: Returns a notification that executes after the method returns the result

@afterthrowing: Exception notification After a method throws an exception

@Around: Surround notification, around method execution

  about the method signature See 5th

2. Front-facing notification

    

3. Post notification: The results of the target method execution cannot be accessed in the post notification

4. Return notification

5. Exception notification

        

6. Surround Notifications

      

      

1  PackageCom.jason.spring.aop.impl;2 3 Importjava.util.Arrays;4 Importjava.util.List;5 6 ImportOrg.aspectj.lang.JoinPoint;7 ImportOrg.aspectj.lang.ProceedingJoinPoint;8 ImportOrg.aspectj.lang.annotation.After;9 Importorg.aspectj.lang.annotation.AfterReturning;Ten Importorg.aspectj.lang.annotation.AfterThrowing; One ImportOrg.aspectj.lang.annotation.Around; A ImportOrg.aspectj.lang.annotation.Aspect; - ImportOrg.aspectj.lang.annotation.Before; - Importorg.springframework.stereotype.Component; the  -  - //declare this class as a slice - //1. The class needs to be placed in the IOC container + @Component - //2. Re-declared as a slice + @Aspect A  Public classLoggingaspect { at      -     //declares that the method is a pre-notification: What classes are executed before the target method starts, and which methods -     //role: @before When the target method is called, and the target method matches the method of the annotation declaration, the AOP framework automatically generates a proxy object for the class where the method is located, and the method to execute the annotation before the target method executes -     //wildcard characters supported -     //@Before ("Execution (public int com.jason.spring.aop.impl.arithmeticcaculatorimpl.* (int, int))" ) -@Before ("Execution (* com.jason.spring.aop.impl.*.* (int, int))") in      Public voidBeforemethod (Joinpoint joinpoint) { -String MethodName =joinpoint.getsignature (). GetName (); tolist<object> args =arrays.aslist (Joinpoint.getargs ()); +System.out.println ("the method" + MethodName + "begins" +args); -     } the      *     /** $      * Panax Notoginseng * @Author: [email protected] - * @Title: Aftermethod the * @Time: December 1, 2016 + * @Description: Code executed after the method executes, regardless of whether the method has an exception A      * the      * @paramJoinpoint +      */ -@After ("Execution (* com.jason.spring.aop.impl.*.* (int, int))") $      Public voidAftermethod (Joinpoint joinpoint) { $String MethodName =joinpoint.getsignature (). GetName (); -list<object> args =arrays.aslist (Joinpoint.getargs ()); -System.out.println ("the method" + MethodName + "End" +args); the     } -     Wuyi     /** the      *  - * @Author: [email protected] Wu * @Title: afterreturning - * @Time: December 1, 2016 About * @Description: Executes the code after the normal end of the method, and put back the notification is a return value that can be accessed to the method $      * -      * @paramJoinpoint -      */ -@AfterReturning (value= "Execution (* com.jason.spring.aop.impl.*.* (..))", returning= "result") A      Public voidafterreturning (joinpoint joinpoint, Object result) { +String MethodName =joinpoint.getsignature (). GetName (); theSystem.out.println ("the method" + MethodName + "End with" +result); -     } $      the     /** the      *  the * @Author: [email protected] the * @Title: afterthrowting - * @Time: December 1, 2016 in * @Description: Executes code when the target method has an exception, can access the exception object, and can specify that the notification code is executed when a specific exception occurs the      * the      * @paramJoinpoint About      * @paramex the      */ the@AfterThrowing (value= "Execution (* com.jason.spring.aop.impl.*.* (..))", throwing= "ex") the      Public voidafterthrowting (Joinpoint joinpoint, Exception ex) { +String MethodName =joinpoint.getsignature (). GetName (); -System.out.println ("the method" + MethodName + "occurs exceptions" +ex); the     }Bayi      the     /** the      *  - * @Author: [email protected] - * @Title: Around the * @Time: December 1, 2016 the * @Description: Surround notifications need to carry Proceedingjoinpoint type parameters the * Surround notification is similar to the whole process of dynamic agents the * Proceedingjoinpoint: Can decide whether to execute the target method - * Surround notification must have a return value, which is the return value of the target method the      *     the      * @paramProceedingjoinpoint the      */94@Around ("Execution (* com.jason.spring.aop.impl.*.* (..))") the      PublicObject Around (proceedingjoinpoint proceedingjoinpoint) { the          theObject result =NULL;98String MethodName =proceedingjoinpoint.getsignature (). GetName (); About          -         //Execute target Method101         Try {102             //front-facing notifications103System.out.println ("the method" + MethodName + "begin with" +arrays.aslist (Proceedingjoinpoint.getargs ()));104              theresult =proceedingjoinpoint.proceed ();106             107             //Post Notification108System.out.println ("the method" + MethodName + "End with" +result);109              the}Catch(Throwable e) {111             //Exception Notification theSystem.out.println ("The method occurs exception:" +e);113             Throw Newruntimeexception (); the         } the             //Post Notification the             117System.out.println ("the method" + MethodName + "End with" +result);118         119         returnresult; -         121     }122     123     124}

    

[Original]java Web learning Note 106:spring Learn---AOP notifications: pre-notification, post-notification, return notification, exception notification, 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.