Spring based on ASPECTJ facet expressions (6)

Source: Internet
Author: User

1PackageCom.proc;23ImportOrg.aspectj.lang.JoinPoint;4ImportOrg.aspectj.lang.ProceedingJoinPoint;5ImportOrg.aspectj.lang.annotation.After;6Importorg.aspectj.lang.annotation.AfterReturning;7Importorg.aspectj.lang.annotation.AfterThrowing;8ImportOrg.aspectj.lang.annotation.Around;9ImportOrg.aspectj.lang.annotation.Aspect;10ImportOrg.aspectj.lang.annotation.Before;11ImportOrg.springframework.stereotype.Component;1213@Aspect14@Component15PublicClassLoggingaspect {16@Before ("Execution (* * * * (int,int))")18PublicvoidBeforemethod (Joinpoint point) {System.out.println ("Executing method:" +Point.getsignature (). GetName ());20}21st@After ("Execution (* * * * (int,int))")23PublicvoidAftermethod (Joinpoint point) {System.out.println ("Method execution End:" +Point.getsignature (). GetName ());25}26@AfterReturning (value= "Execution (* * * * (Int,int))", returning= "RetVal")28PublicvoidAfterreturningmethod (Joinpoint point,object retVal) {System.out.println ("Method:" +point.getsignature (). GetName () + "execution Result:" +RetVal);30}31@AfterThrowing (value= "Execution (* * * * (Int,int))", throwing= "ex")33PublicvoidAfterthrowingmethod (Joinpoint point,exception ex) {System.out.println ("Execution Method:" +point.getsignature (). GetName () + "An exception has occurred:" +Ex.getmessage ());35}36PNS @Around ("Execution (* * * * (int,int))")38PublicObject Aroundmethod (Proceedingjoinpoint point) {39SYSTEM.OUT.PRINTLN ("Surround Notification:" +Point.getsignature (). GetName ());result= ObjectNull;42//This is the equivalent of a pre-notification43Try{44//Execution methodresult=Point.proceed ();46//This is equivalent to the result notification47} catch (Throwable e) { Span style= "COLOR: #008080" >48 // This is equivalent to exception notification 49  E.printstacktrace ();  51 }52 // This is equivalent to the post-notification 53 System.out.println (" Surround notification: "+point.getsignature (). GetName ()); 54 return Result;55 }56}     

Always Specify Execution (* *. * * (Int,int)) when corresponding to the form of the notification, and the modification will be troublesome. To facilitate our introduction of the cut-off form when @pointcut.

Let's look at the code after the change

1PackageCom.proc;23ImportOrg.aspectj.lang.JoinPoint;4ImportOrg.aspectj.lang.ProceedingJoinPoint;5ImportOrg.aspectj.lang.annotation.After;6Importorg.aspectj.lang.annotation.AfterReturning;7Importorg.aspectj.lang.annotation.AfterThrowing;8ImportOrg.aspectj.lang.annotation.Around;9ImportOrg.aspectj.lang.annotation.Aspect;10ImportOrg.aspectj.lang.annotation.Before;11ImportOrg.aspectj.lang.annotation.Pointcut;12ImportOrg.springframework.stereotype.Component;1314@Aspect15@Component16PublicClassLoggingaspect {1718/**Defines a method that declares a slice expression, which is not required in this method. Use is only necessary to refer to the method name*/@Pointcut ("Execution (* * * * (..))")20PublicvoidDeclarejoinpointexpression () {}21st@Before ("Declarejoinpointexpression ()")23PublicvoidBeforemethod (Joinpoint point) {System.out.println ("Executing method:" +Point.getsignature (). GetName ());25}26@After ("Declarejoinpointexpression ()")28PublicvoidAftermethod (Joinpoint point) {System.out.println ("Method execution End:" +Point.getsignature (). GetName ());30}31@AfterReturning (value= "declarejoinpointexpression ()", returning= "RetVal")33PublicvoidAfterreturningmethod (Joinpoint point,object retVal) {System.out.println ("Method:" +point.getsignature (). GetName () + "execution Result:" +RetVal);35}36PNS @AfterThrowing (value= "declarejoinpointexpression ()", throwing= "ex")38PublicvoidAfterthrowingmethod (Joinpoint point,exception ex) {System.out.println ("Execution Method:" +point.getsignature (). GetName () + "An exception has occurred:" +Ex.getmessage ());40}41@Around ("Declarejoinpointexpression ()")43PublicObject Aroundmethod (Proceedingjoinpoint point) {44System.out.println ("Surround Notice:" +Point.getsignature (). GetName ());result= ObjectNull;47//This is the equivalent of a pre-notification48Try{49//Execution methodresult=Point.proceed ();51//This is equivalent to the result notification52} catch (Throwable e) { Span style= "COLOR: #008080" >53 // This is equivalent to exception notification 54  E.printstacktrace ();  56 }57 // This is equivalent to the post-notification 58 System.out.println (" Surround notification: "+point.getsignature (). GetName ()); 59 return Result;60 }61}     

"Note": When you use a slice form in this class, you only need to refer to the method name () to

Other classes in this package: Class name. Method ()

Other classes not in this package: Package name. Class Name. Method Name ()

Spring based on ASPECTJ facet expressions (6)

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.