First, the preface
As for spring AOP, I'm not going to go into this, I'm just a handful of people on the web, and everyone who has used spring knows spring's IOC and AOP. IOC we often use, but in our own system, AOP is used almost zero, except for the small function of this monitoring application, the other is basically not used. The following small series for everyone to organize the use of the spring AOP logging method to execute the time solution, there is a need to look at it together.
Ii. Solutions
1. Traditional methods
The simplest and most outrageous way to do this is to give the time stamp at the beginning and the end of each method that needs to be counted, and then the difference evaluates to the following code:
Long starttime = System.currenttimemillis ();
Business code
Long endtime = System.currenttimemillis ();
In this way, many statistical methods need to be combined with time-consuming code, which has nothing to do with the core business but is repetitive, dispersed, and difficult to maintain.
2. Face-oriented programming method
Therefore, it is not recommended to use the above bad taste code. Having thought about it for a long time, I intend to use the idea of spring AOP to accomplish this function without much to say code and related explanations 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; /** * Detection method executes time-consuming spring slice class * Using the @aspect annotation class, spring will treat it as a special bean (a slice), which is not a dynamic proxy for the class itself * @author Blinkfox * @date 2016- 07-04 */@Aspect @Component public class Timeinterceptor {private static Log logger = Logfactory.getlog (timeintercepto
R.class);
A minute, that is 1000ms private static final long one_minute = 1000; The service layer's statistical time-consuming section, the type must be final string type, the variable to be used in the annotation can only be a static constant type of public static final String point = "Execution" (* Com.blinkfox
. Test.service.impl.*.* (..)) "; /** * Statistical method performs time-consuming Around surround notification * @param joinpoint * @return */@Around (point) public Object Timearound (PROCEEDINGJOINP Oint joinpoint) {//Define return object, parameters required by method object OBJ = null;
object[] args = Joinpoint.getargs ();
Long starttime = System.currenttimemillis ();
try {obj = joinpoint.proceed (args);
catch (Throwable e) {Logger.error ("Statistics a method performs time-consuming surround notification error", E);
//Gets the executed 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 method to execute time-consuming information, if more than a certain amount of time before printing * @param methodname * @param starttime * @param endtime/private void PR
Intexectime (String methodname, Long starttime, long Endtime) {long difftime = Endtime-starttime;
if (Difftime > One_minute) {logger.warn ("-----+ methodname +" method executes time consuming: "+ difftime +" MS "); }
}
}
Note: Finally, you need to applicationContext.xml
add the configuration that AOP requires in your file <aop:aspectj-autoproxy/>
so that spring can recognize it.
Summarize
This is all about using the Spring AOP recording method execution time, I hope the content of this article for everyone's study or work to bring some help, if there is doubt you can message exchange, thank you for the cloud Habitat Community support.