spring-aop-adding logs

Source: Internet
Author: User

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

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.