1 declare a class as a tangent: ① needs to put the class into the IOC, ② is declared as a tangent (@Aspect @Component) @Order (1): Specify the Order
2 Add the following configuration in the configuration file:<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
PackageCOM.ATGUIGU.AOP;Importjava.util.Arrays;ImportOrg.aspectj.lang.JoinPoint;ImportOrg.aspectj.lang.ProceedingJoinPoint;ImportOrg.aspectj.lang.annotation.After;Importorg.aspectj.lang.annotation.AfterReturning;Importorg.aspectj.lang.annotation.AfterThrowing;ImportOrg.aspectj.lang.annotation.Around;ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.aspectj.lang.annotation.Before;ImportOrg.aspectj.lang.annotation.Pointcut;Importorg.springframework.stereotype.Component;/** Declare a class as a slice: 1, you need to put the class in the IOC container; 2, then declare it as a tangent plane **/@Aspect @component Public classLoggingaspect {/*defines a method that declares a pointcut expression, which, in general, does not need to add additional code * Use @pointcut to declare other notifications following the pointcut expression * to refer to the current pointcut expression directly using the method name **/@Pointcut ("Execution (* com.atguigu.aop.*.* (..))") Public voiddeclarejoinpointexpression () {} @Before ("Declarejoinpointexpression ()") Public voidBeforemethod (Joinpoint joinpoint) {String methodName=joinpoint.getsignature (). GetName (); Object [] args=Joinpoint.getargs (); System.out.println ("The method" + MethodName + "begins with" +arrays.aslist (args)); } //Whether or not an exception will be performed@After ("Declarejoinpointexpression ()") Public voidAfter (Joinpoint joinpoint) {String methodName=joinpoint.getsignature (). GetName (); System.out.println ("The method" + MethodName + "End" ); } @AfterReturning (Value= "Declarejoinpointexpression ()", returning= "result") Public voidafterreturning (joinpoint joinpoint,object result) {String methodName=joinpoint.getsignature (). GetName (); System.out.println ("The method" + MethodName + "ends with" +result); } @AfterThrowing (Value= "Declarejoinpointexpression ()", throwing= "ex") Public voidafterthrowing (Joinpoint joinpoint,exception ex) {String methodName=joinpoint.getsignature (). GetName (); System.out.println ("The method" + MethodName + "occurs with" +ex); } /** Surround notifications need to pass a proceedingjoinpoint parameter * Surround notification is similar to the whole process of dynamic agents; proceedingjoinpoint type parameters can decide whether to execute the target method * Surround The notification must have a return value, even if the return value of the target method*///@Around ("Execution (* com.atguigu.aop.*.* (..))")//Public Object Around (proceedingjoinpoint point) {//Object result = null;//String methodName = Point.getsignature (). GetName ();//try {// //front-facing notifications//System.out.println ("The method" +methodname+ "Start with" +arrays.aslist (Point.getargs ()));//result = Point.proceed ();// //Back to Notifications//System.out.println ("The Method" +methodname+ "End With" +result);//} catch (Throwable e) {// //Exception Notification//System.out.println ("The method" +methodname+ "occurs with" +e);//e.printstacktrace ();// }// //Post Notification//System.out.println ("The Method" +methodname+ "End");//return result;// }}
spring-aop-adding logs