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)