Demand:
Suppose you already have some classes, and now you want to count how long each method call takes, what do you do?
Ideas:
My first thought was to go to each method execution before and after recording the current timestamp, and then subtracting statistics to the log.
OK, no problem, so is it reasonable?
First of all, the workload is large and all are repetitive work;
Secondly, the expansion of recruitment is extremely poor;
Again, not elegant, write code not only to consider the completion of requirements, must be in the most elegant form of completion.
So we decided to use spring's aspect-oriented programming technique to assist with this function.
Step: First, create a new Apimonitor.java:
@Aspect Public classapimonitor {@Pointcut ("Execution (* com.spring.service.*.* (..))") Private voidPointcutmethod () {}//declaring a pre-notification@Before ("Pointcutmethod ()") Public voidDobefore () {System.out.println ("Pre-notification"); } //declaring a post-notification@AfterReturning (pointcut = "Pointcutmethod ()", returning = "Result") Public voiddoafterreturning (String result) {System.out.println ("Post Notification"); System.out.println ("---" + result + "---"); } //declaring exception Notifications@AfterThrowing (pointcut = "Pointcutmethod ()", throwing = "E") Public voiddoafterthrowing (Exception e) {System.out.println ("Exception Notification"); System.out.println (E.getmessage ()); } //declaring Final Notice@After ("Pointcutmethod ()") Public voidDoafter () {System.out.println ("Final Notice"); } //declaring surround Notifications@Around ("Pointcutmethod ()") PublicObject Doaround (Proceedingjoinpoint PJP)throwsthrowable {System.out.println ("Enter method---surround Notification"); Object o=pjp.proceed (); System.out.println ("Exit Method---Surround Notification"); returno; } }
This code is copy, because it feels very typical.
For the requirements of this article, you should use surround notification @around this annotation to complete, as long as the Pjp.proceed () before and after the call System.currenttimemillis (), and then subtract, OK.
It is also important to note that the syntax of the excution expression is error-prone, as follows:
Ii. Configuring AOP-related entries in Applicationcontext.xml
class"Org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>
<bean id= "Aspectbean" class= "Com.spring.aop.ApiMonitor"/>
This completes the IOC assembly in spring.
Third, don't forget to introduce the dependency of AOP
Okay, this should be on the 1th, okay, just add in the Pom.xml:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop< /artifactid> <version>4.0.5.RELEASE</version></dependency>
Version depends on the spring version of your project, do not write a version, easy to make mistakes, I have stepped on the pit, Because the 4.x version has more classes than the 3.x version, if it is 4.x AOP to tune 3.x spring may occur problems that the class could not find, directly causing the project to fail to run.
Remember to unify the version number of the project to avoid unnecessary pits!
"Spring" uses AOP for system performance monitoring