生活随笔
收集整理的這篇文章主要介紹了
SpringBoot面向切面编程-用AOP方式管理日志
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
面向切面編程
認(rèn)識AOP
AOP(Aspect Oriented Program,面向切面編程)把業(yè)務(wù)功能分為核心、非核心兩部分。
核心業(yè)務(wù)功能非核心業(yè)務(wù)功能
| 用戶登錄,增加數(shù)據(jù),刪除數(shù)據(jù) | 性能統(tǒng)計(jì),日志,事務(wù)管理 |
在Spring 的面向切面編程(AOP)思想里,非核心業(yè)務(wù)功能被定義為切面。核心業(yè)務(wù)功能和切面功能先被分別進(jìn)行獨(dú)立開發(fā),然后把切面功能和核心業(yè)務(wù)功能編織在一起,這就是AOP。
AOP中的概念
切入點(diǎn)(pointcut):在哪些類、哪些方法上切入通知(advice):在方法前、方法后、方法前后做什么切面(aspect):切面=切入點(diǎn)+通知。也就是在什么時(shí)機(jī)、什么地方,做什么織入(weaving):把切面加入對象,并創(chuàng)建出代理對象的過程環(huán)繞通知:AOP中最強(qiáng)大、靈活的通知,它集成了前置和后置通知,保留了連接點(diǎn)原有的方法。
實(shí)例:用AOP方式管理日志
實(shí)驗(yàn)結(jié)果
登錄網(wǎng)址
http
://localhost
:8080/aoptest
查看到
同時(shí)控制臺顯示信息
URL
:http
://localhost
:8080/aoptestHTTP方法
:GETIP地址:
0:0:0:0:0:0:0:1類的方法:com
.example
.demo
.controller
.AopLogController
.aVoid參數(shù):null應(yīng)答值:hello aop test費(fèi)時(shí):
12
pom.xml添加依賴
這里初學(xué)者踩坑:配置切面@Aspect不管用,是因?yàn)闆]有下面的spring-boot-starter-aop依賴
<!-- SpringBoot 攔截器
--><dependency
><groupId
>org
.springframework
.boot
</groupId
><artifactId
>spring
-boot
-starter
-aop
</artifactId
></dependency
><dependency
><groupId
>org
.apache
.commons
</groupId
><artifactId
>commons
-lang3
</artifactId
></dependency
>
代碼
編寫AOP日志注解類
AopLog.java
下面代碼的解釋:
@Before:在切入點(diǎn)開始處切入內(nèi)容
@After:在切入點(diǎn)結(jié)尾處切入內(nèi)容
@AfterReturning:在切入點(diǎn)返回內(nèi)容之后切入內(nèi)容,可以用來對處理返回值做一些加工處理
@Around:在切入點(diǎn)前后切入內(nèi)容,并控制何時(shí)執(zhí)行切入點(diǎn)自身的內(nèi)容
@AfterThrowing:用來處理當(dāng)切入內(nèi)容部分拋出異常之后的處理邏輯
@Aspect:標(biāo)記為切面類
@Component:把切面類加入IoC容器中,讓Spring來進(jìn)行管理
package com
.example
.demo
.aop
;import org
.apache
.commons
.lang3
.builder
.ToStringBuilder
;
import org
.aspectj
.lang
.JoinPoint
;
import org
.aspectj
.lang
.annotation
.*
;
import org
.slf4j
.Logger
;
import org
.slf4j
.LoggerFactory
;
import org
.springframework
.stereotype
.Component
;
import org
.springframework
.web
. context
.request
.RequestContextHolder
;
import org
.springframework
.web
.context
.request
.ServletRequestAttributes
;import javax
.annotation
.processing
.SupportedSourceVersion
;
import javax
.servlet
.http
.HttpServletRequest
;
import java
.util
.Arrays
;@Aspect@Componentpublic class AopLog {private Logger logger
=LoggerFactory
.getLogger(this.getClass());ThreadLocal
<Long> startTime
=new ThreadLocal<>();@Pointcut("execution(public * com.example..*.*(..))")public void aopWebLog(){}@Before("aopWebLog()")public void doBefore(JoinPoint joinPoint
) throws Throwable
{startTime
.set(System
.currentTimeMillis());ServletRequestAttributes attributes
=(ServletRequestAttributes
)RequestContextHolder
.getRequestAttributes();HttpServletRequest request
=attributes
.getRequest();logger
.info("URL:"+request
.getRequestURL().toString());logger
.info("HTTP方法:"+request
.getMethod());logger
.info("IP地址:"+request
.getRemoteAddr());logger
.info("類的方法:"+joinPoint
.getSignature().getDeclaringTypeName()+"."+joinPoint
.getSignature().getName());logger
.info("參數(shù):"+request
.getQueryString());}@AfterReturning(pointcut
= "aopWebLog()",returning
= "retObject")public void doAfterReturning(Object retObject
) throws Throwable
{logger
.info("應(yīng)答值:"+retObject
);logger
.info("費(fèi)時(shí):"+(System
.currentTimeMillis()-startTime
.get()));}@AfterThrowing(pointcut
= "aopWebLog()",throwing
= "ex")public void addAfterThrowingLogger(JoinPoint joinPoint
,Exception ex
){logger
.error("執(zhí)行 "+" 異常",ex
);}
}
2.控制器
AopLogController.java
package com
.example
.demo
.controller
;import org
.springframework
.web
.bind
.annotation
.GetMapping
;
import org
.springframework
.web
.bind
.annotation
.RestController
;@RestController
public class AopLogController {@GetMapping("/aoptest")public String
aVoid(){return "hello aop test";}
}
總結(jié)
以上是生活随笔為你收集整理的SpringBoot面向切面编程-用AOP方式管理日志的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。