1. Preface
I won’t introduce spring aop. I searched a lot online. Anyone who has used spring knows spring’s ioc and aop. We often use ioc, but in our own system, the use of aop is almost zero. Except for this small monitoring function that is applied, the others are basically not used. Below, the editor will sort out the solution to using Spring AOP recording method to execute time. Let’s take a look if you need it.
2. Solution
1. Traditional methods
The simplest and rough method is to add the timestamp at the beginning and end of each method that needs statistics, and then calculate the difference. The code is as follows:
long startTime = System.currentTimeMillis();// Business code long endTime = System.currentTimeMillis(); System.out.println("Program run time:" + (endTime - startTime) + "ms"); //Output program run timeThis method requires time-consuming code to be added to many statistical methods. These codes have nothing to do with the core business, but are repeated in large quantities and scattered everywhere, making it difficult to maintain.
2. Methods of tangent-oriented programming
Therefore, it is not recommended to use the above bad taste code. I thought about it for a long time and planned to use Spring AOP's ideas to complete this function. Without further ado, the code and related explanations are as follows:
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component;/** * Time-consuming spring facet class for detection method execution* Using the class annotated by @Aspect, Spring will treat it as a special bean (a section), that is, it will not dynamically proxy the class itself* @author blinkfox * @date 2016-07-04 */@Aspect@Componentpublic class TimeInterceptor { private static Log logger = LogFactory.getLog(TimeInterceptor.class); // One minute, that is, 1000ms private static final long ONE_MINUTE = 1000; // The statistical time-consuming section of the service layer must be final For String type, the variables to be used in the annotation can only be static constant type public static final String POINT = "execution (* com.blinkfox.test.service.impl.*.*(..))"; /** * Time-consuming execution of statistical method Around surrounding notification* @param joinPoint * @return */ @Around(POINT) public Object timeAround(ProceedingJoinPoint joinPoint) { // Define the parameters required to return the object and get the method. Object obj = null; Object[] args = joinPoint.getArgs(); long startTime = System.currentTimeMillis(); try { obj = joinPoint.proceed(args); } catch (Throwable e) { logger.error("Statistics errors in the execution time-consuming surround notifications", e); } // Get the execution method name long endTime = System.currentTimeMillis(); MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String methodName = signature.getDeclaringTypeName() + "." + signature.getName(); // Print time-consuming information this.printExecTime(methodName, startTime, endTime); return obj; } /** * Print time-consuming information that prints the method execution. If it exceeds a certain time, print* @param methodName * @param startTime * @param endTime */ private void printExecTime(String methodName, long startTime, long endTime) { long diffTime = endTime - startTime; if (diffTime > ONE_MINUTE) { logger.warn("-----" + methodName + " method execution time: " + diffTime + " ms"); } }} Note: Finally, you need to add the configuration required by AOP in the applicationContext.xml file <aop:aspectj-autoproxy/> so that Spring can recognize it.
Summarize
The above is all about using Spring AOP to record the execution time. I hope the content of this article will be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.