本文已参与「新人创作礼」活动,一起开启掘金创作之路
Spring有两个特性需要我们去了解 其中一个是AOP 另一个是IOC
其中AOP是 面向切面编程(AOP)就是纵向的编程。比如业务A和业务B现在需要一个相同的操作,传统方法我们可能需要在A、B中都加入相关操作代码,而应用AOP就可以只写一遍代码,A、B共用这段代码。并且,当A、B需要增加新的操作时,可以在不改动原代码的情况下,灵活添加新的业务逻辑实现。
其中有几个注解需要我们了解
1.@Aspect:是程序共有功能的实现。在实际开发中通常是一个存放共有功能实现的标准Java类。当Java类使用了 @Aspect注解修饰时,就能被AOP容器识别为切面。
2.通过(advice):这是@Aspect的具体实现 其中有@Before("切入点方法") @Around("切入点方法") @AfterReturning("切入点方法") 其中从名字就能知道是在切入点方法之前 环绕 和之后操作的实现 通过注解名区分
3.@Pointcut("切入的连接点")
懂了上面之后就可以开始使用Aop记录Log信息
代码如下
package com.xkcoding.log.aop.aspectj;
import cn.hutool.json.JSONUtil;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;
@Aspect
@Component
@Slf4j
public class AopLog {
private static final String START_TIME = "request-start";
/**
* 切入点
*/
@Pointcut("execution(public * com.zl.log.aop.controller.*Controller.*(..))")
public void log() {
System.out.println("qrd");
}
/**
* 前置操作
*
* @param point 切入点
*/
@Before("log()")
public void beforeLog(JoinPoint point) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
log.info("【请求 URL】:{}", request.getRequestURL());
log.info("【请求 IP】:{}", request.getRemoteAddr());
log.info("【请求类名】:{},【请求方法名】:{}", point.getSignature().getDeclaringTypeName(), point.getSignature().getName());
Map<String, String[]> parameterMap = request.getParameterMap();
log.info("【请求参数】:{},", JSONUtil.toJsonStr(parameterMap));
Long start = System.currentTimeMillis();
request.setAttribute(START_TIME, start);
}
/**
* 环绕操作
*
* @param point 切入点
* @return 原方法返回值
* @throws Throwable 异常信息
*/
@Around("log()")
public Object aroundLog(ProceedingJoinPoint point) throws Throwable {
Object result = point.proceed();
log.info("【返回值】:{}", JSONUtil.toJsonStr(result));
return result;
}
/**
* 后置操作
*/
@AfterReturning("log()")
public void afterReturning() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
Long start = (Long) request.getAttribute(START_TIME);
Long end = System.currentTimeMillis();
log.info("【请求耗时】:{}毫秒", end - start);
String header = request.getHeader("User-Agent");
UserAgent userAgent = UserAgent.parseUserAgentString(header);
log.info("【浏览器类型】:{},【操作系统】:{},【原始User-Agent】:{}", userAgent.getBrowser().toString(), userAgent.getOperatingSystem().toString(), header);
}
}
而记录的日志为
2021-02-03 18:43:19,929 [http-nio-8080-exec-1] INFO [com.zl.log.aop.aspectj.AopLog] AopLog.java:55 - 【请求 URL】:http://localhost:8080/demo/test
2021-02-03 18:43:19,929 [http-nio-8080-exec-1] INFO [com.zl.log.aop.aspectj.AopLog] AopLog.java:56 - 【请求 IP】:0:0:0:0:0:0:0:1
2021-02-03 18:43:19,931 [http-nio-8080-exec-1] INFO [com.zl.log.aop.aspectj.AopLog] AopLog.java:57 - 【请求类名】:com.xkcoding.log.aop.controller.TestController,【请求方法名】:test
2021-02-03 18:43:19,938 [http-nio-8080-exec-1] INFO [com.zl.log.aop.aspectj.AopLog] AopLog.java:60 - 【请求参数】:{},
2021-02-03 18:43:19,952 [http-nio-8080-exec-1] INFO [com.zl.log.aop.aspectj.AopLog] AopLog.java:75 - 【返回值】:{"who":"me"}
2021-02-03 18:43:19,952 [http-nio-8080-exec-1] INFO [com.zl.log.aop.aspectj.AopLog] AopLog.java:89 - 【请求耗时】:13毫秒
2021-02-03 18:43:19,964 [http-nio-8080-exec-1] INFO [com.zl.log.aop.aspectj.AopLog] AopLog.java:93 - 【浏览器类型】:CHROME,【操作系统】:WINDOWS_10,【原始User-Agent】:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3756.400 QQBrowser/10.5.4039.400
2021-02-03 19:02:42,206 [http-nio-8080-exec-5] INFO [com.zl.log.aop.aspectj.AopLog] AopLog.java:55 - 【请求 URL】:http://localhost:8080/demo/test
2021-02-03 19:02:42,207 [http-nio-8080-exec-5] INFO [com.zl.log.aop.aspectj.AopLog] AopLog.java:56 - 【请求 IP】:0:0:0:0:0:0:0:1
2021-02-03 19:02:42,208 [http-nio-8080-exec-5] INFO [com.zl.log.aop.aspectj.AopLog] AopLog.java:57 - 【请求类名】:com.xkcoding.log.aop.controller.TestController,【请求方法名】:test
2021-02-03 19:02:42,208 [http-nio-8080-exec-5] INFO [com.zl.log.aop.aspectj.AopLog] AopLog.java:60 - 【请求参数】:{},
2021-02-03 19:02:42,209 [http-nio-8080-exec-5] INFO [com.zl.log.aop.aspectj.AopLog] AopLog.java:75 - 【返回值】:{"who":"me"}
2021-02-03 19:02:42,210 [http-nio-8080-exec-5] INFO [com.zl.log.aop.aspectj.AopLog] AopLog.java:89 - 【请求耗时】:1毫秒
2021-02-03 19:02:42,210 [http-nio-8080-exec-5] INFO [com.zl.log.aop.aspectj.AopLog] AopLog.java:93 - 【浏览器类型】:CHROME,【操作系统】:WINDOWS_10,【原始User-Agent】:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3756.400 QQBrowser/10.10.1
从这记录的日志信息就可以很方便的看出切面的切入点 和方法之间的使用