AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是Spring框架中的一个重要内容,它通过对既有程序定义一个切入点,然后在其前后切入不同的执行内容,比如常见的有:打开数据库连接/关闭数据库连接、打开事务/关闭事务、记录日志等。基于AOP不会破坏原来程序逻辑,因此它可以很好的对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
需要对web请求来记录日志,所以需要引入依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.1.1.RELEASEversion>
dependency>
引入aop依赖:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-aopartifactId>
<version>2.1.1.RELEASEversion>
dependency>
查看Aop自动配置类代码可以知道:在完成了引入AOP依赖包后,一般来说并不需要去做其他配置。也许在Spring中使用过注解配置方式的人会问是否需要在程序主类中增加@EnableAspectJAutoProxy来启用,实际并不需要。
可以看下面关于AOP的默认配置属性,其中spring.aop.auto属性默认是开启的,也就是说只要引入了AOP依赖后,默认已经增加了@EnableAspectJAutoProxy。
AOP spring.aop.auto=true # Add @EnableAspectJAutoProxy.
spring.aop.proxy-target-class=false # Whether subclass-based (CGLIB) proxies are to be created (true) as opposed to standard Java interface-based proxies (false).
我在做测试的时候,以上两个配置我都没有进行使用,请自行进行测试。
而当我们需要使用CGLIB来实现AOP的时候,需要配置spring.aop.proxy-target-class=true,不然默认使用的是标准Java的实现。
注解解释:
实现AOP的切面主要有以下几个要素:
@Around 来对controller 进行切面:
package com.pica.cloud.foundation.utils.aop;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@Aspect
@Configuration
public class PicaLogAspect {
private static final Logger logger = LoggerFactory.getLogger(PicaLogAspect.class);
ThreadLocal<Long> startTime = new ThreadLocal();
ThreadLocal<HttpServletRequest> request = new ThreadLocal();
public PicaLogAspect() {
}
@Pointcut("execution(public * com.pica.cloud..*Controller.*(..))")
public void weblog() {
}
@Around("weblog()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes)ra;
HttpServletRequest request = sra.getRequest();
this.request.set(request);
String url = request.getRequestURL().toString();
String method = request.getMethod();
String uri = request.getRequestURI();
String queryString;
if ("GET".equalsIgnoreCase(method)) {
queryString = request.getQueryString();
} else if (!this.parseMultipart()) {
queryString = this.getParamsStringFromJsonBody();
} else {
queryString = "";
}
this.startTime.set(System.currentTimeMillis());
logger.info("request start, controller params==>, url: {}, method: {}, uri: {}, params: {}", new Object[]{url, method, uri, queryString});
Object result = pjp.proceed();
new Gson();
String res = "";
logger.info("request spent time milliSeconds ==>" + (System.currentTimeMillis() - ((Long)this.startTime.get()).longValue()));
return result;
}
protected Map<String, Object> getBeanFromJsonBody() throws Exception {
Gson gson = new Gson();
return (Map)gson.fromJson(IOUtils.toString(new InputStreamReader(((HttpServletRequest)this.request.get()).getInputStream(), "utf-8")), (new TypeToken<Map<String, Object>>() {
}).getType());
}
protected String getParamsStringFromJsonBody() throws Exception {
InputStream is = ((HttpServletRequest)this.request.get()).getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
String res = IOUtils.toString(isr);
isr.close();
is.close();
return res;
}
protected boolean parseMultipart() throws Exception {
return ServletFileUpload.isMultipartContent((HttpServletRequest)this.request.get());
}
public static String getBodyString(BufferedReader br) {
String str = "";
try {
String inputLine;
while((inputLine = br.readLine()) != null) {
str = str + inputLine;
}
br.close();
} catch (IOException var4) {
System.out.println("IOException: " + var4);
}
return str;
}
public static String getHeaderString(HttpServletRequest request) {
JSONObject jsonObject = new JSONObject();
Enumeration enu = request.getHeaderNames();
String str;
while(enu.hasMoreElements()) {
str = (String)enu.nextElement();
String headerValue = request.getHeader(str);
jsonObject.put(str, headerValue);
System.out.println(str + ":" + headerValue);
}
str = jsonObject.toJSONString();
return str;
}
}
使用@Before和@AfterReturning实现:
import java.util.Arrays;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* 实现Web层的日志切面
* @author
* @version v.0.1
*/
@Aspect
@Component
@Order(-5)
public class WebLogAspect {
private Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 定义一个切入点.
* 解释下:
*
* ~ 第一个 * 代表任意修饰符及任意返回值.
* ~ 第二个 * 任意包名
* ~ 第三个 * 代表任意方法.
* ~ 第四个 * 定义在web包或者子包
* ~ 第五个 * 任意方法
* ~ .. 匹配任意数量的参数.
*/
@Pointcut("execution(public * com.kfit.*.web..*.*(..))")
publicvoid webLog(){}
@Before("webLog()")
publicvoid doBefore(JoinPoint joinPoint){
// 接收到请求,记录请求内容
logger.info("WebLogAspect.doBefore()");
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
logger.info("URL : " + request.getRequestURL().toString());
logger.info("HTTP_METHOD : " + request.getMethod());
logger.info("IP : " + request.getRemoteAddr());
logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
//获取所有参数方法一:
Enumeration<String> enu=request.getParameterNames();
while(enu.hasMoreElements()){
String paraName=(String)enu.nextElement();
System.out.println(paraName+": "+request.getParameter(paraName));
}
}
@AfterReturning("webLog()")
publicvoid doAfterReturning(JoinPoint joinPoint){
// 处理完请求,返回内容
logger.info("WebLogAspect.doAfterReturning()");
}
}
两种方法都是能够对请求数据做日志监控。第一种方式和第二种方式有一些不同,第二种方式使用的是@Around 环绕的方式去做的处理,joinPoint.proceed()返回数据需要等方法执行完才能执行下面的代码,这种是阻塞式的请求