文章來源:https://macrozheng.github.io/mall-learning/#/technology/aop_log
AOP
AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預編譯方式和運行期動態代理實現程序功能的統一維護的一種技術。利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發的效率。
AOP的相關術語
通知(Advice)
通知描述了切面要完成的工作以及何時執行。比如我們的日志切面需要記錄每個接口調用時長,就需要在接口調用前后分別記錄當前時間,再取差值。
- 前置通知(Before):在目標方法調用前調用通知功能;
- 后置通知(After):在目標方法調用之后調用通知功能,不關心方法的返回結果;
- 返回通知(AfterReturning):在目標方法成功執行之后調用通知功能;
- 異常通知(AfterThrowing):在目標方法拋出異常后調用通知功能;
- 環繞通知(Around):通知包裹了目標方法,在目標方法調用之前和之后執行自定義的行為。
連接點(JoinPoint)
通知功能被應用的時機。比如接口方法被調用的時候就是日志切面的連接點。
切點(Pointcut)
切點定義了通知功能被應用的范圍。比如日志切面的應用范圍就是所有接口,即所有controller層的接口方法。
切面(Aspect)
切面是通知和切點的結合,定義了何時、何地應用通知功能。
引入(Introduction)
在無需修改現有類的情況下,向現有的類添加新方法或屬性。
織入(Weaving)
把切面應用到目標對象並創建新的代理對象的過程。
Spring中使用注解創建切面
相關注解
- @Aspect:用於定義切面
- @Before:通知方法會在目標方法調用之前執行
- @After:通知方法會在目標方法返回或拋出異常后執行
- @AfterReturning:通知方法會在目標方法返回后執行
- @AfterThrowing:通知方法會在目標方法拋出異常后執行
- @Around:通知方法會將目標方法封裝起來
- @Pointcut:定義切點表達式
切點表達式
指定了通知被應用的范圍,表達式格式:
execution(方法修飾符 返回類型 方法所屬的包.類名.方法名稱(方法參數)
//com.macro.mall.tiny.controller包中所有類的public方法都應用切面里的通知 execution(public * com.macro.mall.tiny.controller.*.*(..)) //com.macro.mall.tiny.service包及其子包下所有類中的所有方法都應用切面里的通知 execution(* com.macro.mall.tiny.service..*.*(..)) //com.macro.mall.tiny.service.PmsBrandService類中的所有方法都應用切面里的通知 execution(* com.macro.mall.tiny.service.PmsBrandService.*(..))
添加AOP切面實現接口日志記錄
添加日志信息封裝類WebLog
用於封裝需要記錄的日志信息,包括操作的描述、時間、消耗時間、url、請求參數和返回結果等信息。
package com.xc.mall2.dto; import lombok.Getter; import lombok.Setter; import lombok.ToString; /** * Controller層的日志封裝類 * Created by xc on 190903. */ @Getter @Setter @ToString public class WebLog { /** * 操作描述 */ private String description; /** * 操作用戶 */ private String username; /** * 操作時間 */ private Long startTime; /** * 消耗時間 */ private Integer spendTime; /** * 根路徑 */ private String basePath; /** * URI */ private String uri; /** * URL */ private String url; /** * 請求類型 */ private String method; /** * IP地址 */ private String ip; /** * 請求參數 */ private Object parameter; /** * 請求返回的結果 */ private Object result; }
添加切面類WebLogAspect
定義了一個日志切面,在環繞通知中獲取日志需要的信息,並應用到controller層中所有的public方法中去。
package com.xc.mall2.component; import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.URLUtil; import cn.hutool.json.JSONUtil; import com.xc.mall2.dto.WebLog; import io.swagger.annotations.ApiOperation; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.lang.reflect.Parameter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 統一日志處理切面 * Created by xc on 190903. */ @Aspect @Component @Order(1) public class WebLogAspect { private static final Logger LOGGER = LoggerFactory.getLogger(WebLogAspect.class); @Pointcut("execution(public * com.xc.mall2.controller.*.*(..))") public void webLog() { } @Before("webLog()") public void doBefore(JoinPoint joinPoint) throws Throwable { } @AfterReturning(value = "webLog()", returning = "ret") public void doAfterReturning(Object ret) throws Throwable { } @Around("webLog()") public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { long startTime = System.currentTimeMillis(); //獲取當前請求對象 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); //記錄請求信息 WebLog webLog = new WebLog(); Object result = joinPoint.proceed(); Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); if (method.isAnnotationPresent(ApiOperation.class)) { ApiOperation apiOperation = method.getAnnotation(ApiOperation.class); webLog.setDescription(apiOperation.value()); } long endTime = System.currentTimeMillis(); String urlStr = request.getRequestURL().toString(); webLog.setBasePath(StrUtil.removeSuffix(urlStr, URLUtil.url(urlStr).getPath())); webLog.setIp(request.getRemoteUser()); webLog.setMethod(request.getMethod()); webLog.setParameter(getParameter(method, joinPoint.getArgs())); webLog.setResult(result); webLog.setSpendTime((int) (endTime - startTime)); webLog.setStartTime(startTime); webLog.setUri(request.getRequestURI()); webLog.setUrl(request.getRequestURL().toString()); LOGGER.info("{}", JSONUtil.parse(webLog)); return result; } /** * 根據方法和傳入的參數獲取請求參數 */ private Object getParameter(Method method, Object[] args) { List<Object> argList = new ArrayList<>(); Parameter[] parameters = method.getParameters(); for (int i = 0; i < parameters.length; i++) { //將RequestBody注解修飾的參數作為請求參數 RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class); if (requestBody != null) { argList.add(args[i]); } //將RequestParam注解修飾的參數作為請求參數 RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class); if (requestParam != null) { Map<String, Object> map = new HashMap<>(); String key = parameters[i].getName(); if (!StringUtils.isEmpty(requestParam.value())) { key = requestParam.value(); } map.put(key, args[i]); argList.add(map); } } if (argList.size() == 0) { return null; } else if (argList.size() == 1) { return argList.get(0); } else { return argList; } } }
接口測試:
2019-09-03 16:48:31.866 INFO 14208 --- [nio-8080-exec-2] com.xc.mall2.component.WebLogAspect : {"result":{"code":200,"data":{"total":11,"totalPage":11,"pageSize":1,"list":[{"productCommentCount":100,"name":"萬和","bigPic":"","logo":"http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg(5).jpg","showStatus":1,"id":1,"sort":0,"productCount":100,"firstLetter":"W","factoryStatus":1}],"pageNum":1},"message":"操作成功"},"basePath":"http://localhost:8080","method":"GET","ip":"test","parameter":[{"pageNum":1},{"pageSize":1}],"description":"分頁查詢品牌列表","startTime":1567500511861,"uri":"/brand/list","url":"http://localhost:8080/brand/list","spendTime":5}
