spring boot aop 记录方法执行时间
生活随笔
收集整理的這篇文章主要介紹了
spring boot aop 记录方法执行时间
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
了性能調(diào)優(yōu),需要先統(tǒng)計(jì)出來(lái)每個(gè)方法的執(zhí)行時(shí)間,直接在方法前后log輸出太麻煩,可以用AOP來(lái)加入時(shí)間統(tǒng)計(jì)
?
添加依賴(lài)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>在application.properties中加入配置
spring.aop.auto=true實(shí)現(xiàn)具體代碼
?
@Component @Aspect public class LogAspect { private static final Log LOG = LogFactory.getLog(LogAspect.class); /** * 定義一個(gè)切入點(diǎn). * 解釋下: * * ~ 第一個(gè) * 代表任意修飾符及任意返回值. * ~ 第二個(gè) * 定義在web包或者子包 * ~ 第三個(gè) * 任意方法 * ~ .. 匹配任意數(shù)量的參數(shù). */ @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; } } }注意問(wèn)題
-
aop后方法不能正確返回值
//這樣是不對(duì)的public void doAround(ProceedingJoinPoint joinPoint){}
這個(gè)代理方法一定要返回值,否則,在代碼中就沒(méi)有返回值了。 -
Spring的文檔中這么寫(xiě)的:Spring AOP部分使用JDK動(dòng)態(tài)代理或者CGLIB來(lái)為目標(biāo)對(duì)象創(chuàng)建代理。如果被代理的目標(biāo)實(shí)現(xiàn)了至少一個(gè)接口,則會(huì)使用JDK動(dòng)態(tài)代理。所有該目標(biāo)類(lèi)型實(shí)現(xiàn)的接口都將被代理。若該目標(biāo)對(duì)象沒(méi)有實(shí)現(xiàn)任何接口,則創(chuàng)建一個(gè)CGLIB代理。
默認(rèn)是JDK動(dòng)態(tài)代理,更改為cglib
-
?
轉(zhuǎn)載于:https://www.cnblogs.com/alantong08/p/7099468.html
總結(jié)
以上是生活随笔為你收集整理的spring boot aop 记录方法执行时间的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ssh公钥免密码登录
- 下一篇: 新型互联网交换中心促进互联网产业发展,助