Spring boot AOP Logging method Execution Time

Source: Internet
Author: User
Tags add time aop throwable
Preface

In order to achieve performance tuning, you need to first count the execution time of each method, directly before and after the log output is too cumbersome, you can use AOP to add time statistics added dependencies

<dependency>    
 <groupId>org.springframework.boot</groupId>    
 <artifactId> Spring-boot-starter-aop</artifactid>
</dependency>
add configuration to Application.properties
Spring.aop.auto=true

The Spring.aop.auto property is turned on by default, which means that as long as the AOP dependency is introduced, the default has been increased by @enableaspectjautoproxy. Remember to never add extra information, such as @enableaspectjautoproxy. Implementing specific Code

@Component @Aspect public class Logaspect {private static final log = Logfactory.getlog (Logaspect.class);
     /** * defines a pointcut.
     * Explanation: * * ~ the first * represents any modifier and any return value. * ~ the second * is defined in the Web package or the sub package * ~ Third * Any method * ~ ...
     Matches any number of parameters. * * @Pointcut (* Execution (* com.wedo.stream.service).
     *.*(..))") public void Logpointcut () {} @org. Aspectj.lang.annotation.Around ("Logpointcut ()") Public Object Doaround (Proceed
 		Ingjoinpoint joinpoint) throws throwable{//Log.debug ("logpointcut" + joinpoint + "T");
 		Long start = System.currenttimemillis ();
 			try {Object result = Joinpoint.proceed ();
 			Long end = System.currenttimemillis ();
 			Log.error ("+++++around" + joinpoint + "\tuse Time:" + (End-start) + "ms!"); 

 		return result;
 			catch (Throwable e) {Long end = System.currenttimemillis ();
 			Log.error ("+++++around" + joinpoint + "\tuse Time:" + (End-start) + "Ms with exception:" + E.getmessage ());
 Throw e;		}

     }

} 
pay attention to the problem

Post-AOP methods do not return values correctly
This proxy method must return a value, otherwise there will be no return value in the code.

This is wrong. Public
  void Doaround (Proceedingjoinpoint joinpoint) {}

This is written in spring's documentation: the Spring AOP section uses JDK dynamic proxies or cglib to create proxies for target objects. If the target of the proxy implements at least one interface, the JDK dynamic proxy is used. All interfaces implemented by the target type will be represented. If the target object does not implement any interfaces, a CGLIB proxy is created.
The default is JDK dynamic proxy, changed to Cglib

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.