The main research in this paper is the implementation code of the execution time of the spring boot aop recording method, as follows.
In order to optimize performance, it is necessary to count the execution time of each method first. It is too troublesome to directly output the log before and after the method. You can use AOP to add time statistics
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId></dependency>
spring.aop.auto=true
The spring.aop.auto property is enabled by default, which means that as long as the AOP dependency is introduced, @EnableAspectJAutoProxy has been added by default. Remember not to add unnecessary information, such as @EnableAspectJAutoProxy!
@Component@Aspectpublic class LogAspect {private static final Log LOG = LogFactory.getLog(LogAspect.class);/** * Define an entry point. * Explanation: * * ~ The first * represents any modifier and any return value. * ~ The second * is defined in a web package or subpackage* ~ The third * any method* ~ .. Match any number of parameters. */@Pointcut("execution(* com.wedo.stream.service..*.*(..))") public void logPointcut(){}@org.aspectj.lang.annotation.Around("logPointcut()") public Object doAround(ProceedingJoinPoint 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;}}} After aop method cannot return the value 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){} Spring's documentation says this: The Spring AOP part uses JDK dynamic proxy or CGLIB to create proxy for the target object. If the proxy target implements at least one interface, a JDK dynamic proxy is used. All interfaces implemented by this target type will be proxyed. If the target object does not implement any interface, a CGLIB proxy is created.
The default is JDK dynamic proxy, changed to cglib
The above is all the content of this article about the execution time code example of spring boot aop recording method. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!