Calculating service execution time using AOP implementations in Springboot

Source: Internet
Author: User
Tags aop throwable

1, increase the pom.xml of the dependency rack package

<!--Introducing Spring AOP Dependency--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2. Writing AOP Tangent body classes
such as: Logservicetaketime
1  PackageCOM.LEECX.AOP;2 3 ImportOrg.aspectj.lang.JoinPoint;4 ImportOrg.aspectj.lang.ProceedingJoinPoint;5 Importorg.aspectj.lang.annotation.*;6 ImportOrg.slf4j.Logger;7 Importorg.slf4j.LoggerFactory;8 Importorg.springframework.stereotype.Component;9 Ten /** One * <p>Title:</p> A * How much time does it take to execute the <p>description:service method </p> - * <p>copyright:copyright (c) 2018</p> - * <p>company: Neusoft Group Co., Ltd. </p> the * CREATEDATE:2018/4/18 0018 pm 23:09 - * Author: section Merrill Lynch [[email protected]] - * version:1.0 -  */ + //declare the principal class, define the subject class of the plane - @Aspect + @Component A  Public classLogservicetaketime { at      -     Private Final StaticLogger log = Loggerfactory.getlogger (logservicetaketime.class); -  -@Pointcut ("Execution (* com.leecx.service). *.*(..))") -      Public voidperformance () { -     } in  -@Around ("Performance ()") to      PublicObject Dolog (Proceedingjoinpoint joinpoint)throwsThrowable { +          -         //Record start time the         LongBegin =System.currenttimemillis (); *Object result = ""; $         /**Execute target Method*/Panax Notoginseng         Try{ -result=joinpoint.proceed (); the         } +         Catch(Exception e) { ALog.error ("Log record error, errormessage: {}", E.getmessage ()); the         } +         finally{ -             /**Record operation time*/ $             Longtook = System.currenttimemillis ()-begin; $             if(Took >= 10000) { -Log.error ("Service execution time: {} seconds", took); - //log.error ("Controller execution time: {} milliseconds", took); the}Else if(Took >= 5000) { -Log.warn ("Service execution time: {} seconds", took);Wuyi //Log.warn ("Controlle R execution time: {} milliseconds", took); the}Else  if(Took >= 3000) { -Log.info ("Service execution time: {} seconds", took); Wu //log.info ("Controller execution time: {} milliseconds", took); -             } About             //TODO logs are saved to MongoDB $         } -         returnresult; -     } -      A@Before ("Performance ()") +      Public voidDobefore (Joinpoint joinpoint)throwsThrowable { the         //receive request, log request content -Log.info ("Dobefore"); $     } the      the@AfterReturning (returning = "ret", Pointcut = "performance ()") the      Public voidDoafterreturning (Object ret)throwsThrowable { the         //after processing the request, return the content -Log.info ("Doafterreturning"); in     } the      the}
Description:
1. Add annotations on the body of the class to define the facets and declare @Aspect defined facets
2, @Pointcut ("Execution (* com.leecx.service). *.*(..))") Defining pointcuts, generally using expressions to declare the scope of a cut
all methods, such as the Com.leecx.service package, will be intercepted to
3. @Before: The method of pre-processing before starting point
4. @After: The method of execution at the end of Pointcuts
5. @AfterReturning: The method executed after the Pointcut return data (typically used to wrap the returned data)
6. @Around: Methods to be executed before and after Pointcuts
7. @AfterThrowing: Throws Exception Execution method

Calculating service execution time using AOP implementations in Springboot

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.