在接口开发中,我们通常需要统计接口耗时,为后续接口性能做统计。在springMVC中可以用它的aop来记录日志。
1、在spring配置文件中开启AOP
<!--*************** 支持aop **************** -->
<aop:aspectj-autoproxy proxy-target-class="true" />
2、编写AOP
package com.parry.demo.aop; import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component; /**
* <p>
* PerformanceInterceptor 性能统计接口:接口耗时统计
* <p>*/
@Aspect
@Component
public class PerformanceInterceptor { private Logger log = Logger.getLogger(PerformanceInterceptor.class); //我需要监控所有controlller的耗时(controller放在包com.parry.demo.controller下面)
@Around("execution(* com.parry.demo.controller.*.*(..))")
public Object logTome(ProceedingJoinPoint pjp) throws Throwable {
long begin = System.currentTimeMillis();
String method = pjp.getSignature().getName();
String className = pjp.getTarget().getClass().getName(); Object ret = pjp.proceed();
log.info("func<doAround> method<" + className + "." + method + "> cost time <"
+ (System.currentTimeMillis() - begin) + ">ms");
return ret;
}
}
3、测试访问接口
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABakAAAAVCAIAAADD6PU0AAAMcUlEQVR4nO1dyYHjMAxLXS7I9aQaN5Niso9cOgiQoo94ssBrJ7JECjxEMZ6dy10QBEEQBEEQBEEQBOF3cfm2AoIgCIIgCIIgCIIgCDtCvQ9BEARBEARBEARBEH4Z/2nvY5kvb8xLeNrtOmWmHYfkvoQaBY1iURhAGYDT9fZtdYQ/j1N51DL/4Yx4QuU3UemE+/pzEIcQpBj6gTppmc+QV7fESey1k6zfs9caiI0V+PQ+nvf63k8HnbjsD/RTykouaDSyYDPUrBjoVAweebfrZD1ebarS4zFSTCkFdtMq7Ve1MYaPcmh9PMToLZRvjUyGgDhs5Z74elEsC/vh7ToV0uNE7lA9IeYdClHAgmnp+MqpQTKA5xoA49Nav9nIbrXr/AJwZovMPDcV59fwfB41xNnDec5TmX3T4EC2eh8nwU9xuMtmyKKHkHe7T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SgIWsZGKr7QaKAM4Cwa5CtLLw6NVJXr1hSdLRWNL/XCR3ZC7FDkpUmOZdkFbxpnNk4V9IEnvauX7pZGGw/baC6wofqt328IB6mfs9PB8KQ5lZnas0c6IvTGciga2jJVP1E5eZQYduxsNBlGEjd4otUh27vk+H+59mPd8W3ecvlb0PrxWvDHGOLRN6bGB7UXUGDdKOvemQq8/Asjp4HPoqQGJ6guE5u0hk/lexYDyHr0xaf+3vbis4+yVviOPR2VDILovo9GIIuha8dFk3SEV08Y6X7stsVsbMorfuGH0juLiP3IoaGdpH3Hrex/nw9FF+m9gyLxBimUJQfgKFHqC8BUcH3obfA8o/BrMb8V1JpwWspdwHM7V+yDN+32g3odQYpnLztvtOq9+ZUiWEISvQKEnCF/BWV6QEv5ndF7BXhIWvg7ZSzgQ5+l90N8a20fY8Fs/idfrj8W635QR7jWF3v88o96HIJwVCj1B+AoOC713RaZAFwy0vxsgNzk3ZC/hKJyn9yEIgiAIgiAIgiAIgrA91PsQBEEQBEEQBEEQBOGXod6HIAiCIAiCIAiCIAi/DPU+BEEQBEEQBEEQBEH4ZfwDljXJLczxAhEAAAAASUVORK5CYII=" alt="" />
4、关于AOP的一些说明
(1)一些常见的切入点
- execution(public * * (. .)) 任意公共方法被执行时,执行切入点函数
- execution( * set* (. .)) 任何以一个“set”开始的方法被执行时,执行切入点函数
- execution( * com.demo.service.AccountService.* (. .)) 当接口AccountService 中的任意方法被执行时,执行切入点函数
- execution( * com.demo.service.*.* (. .)) 当service 包中的任意方法被执行时,执行切入点函数
- within(com.demo.service.*) 在service 包里的任意连接点
- within(com.demo.service. .*) 在service 包或子包的任意连接点
- this(com.demo.service.AccountService) 实现了AccountService 接口的代理对象的任意连接点
- target(com.demo.service.AccountService) 实现了AccountService 接口的目标对象的任意连接点
- args(Java.io.Serializable) 任何一个只接受一个参数,且在运行时传入参数实现了 Serializable 接口的连接点
(2)AOP多样的方法
- @Before:方法前执行
- @AfterReturning:运行方法后执行
- @AfterThrowing:Throw后执行
- @After:无论方法以何种方式结束,都会执行(类似于finally)
- @Around:环绕执行