aop就是从一个切面来处理相同的东西,如好多业务都有同一个方法等,你可以对此进行拦截。
首先定义一个切面,在里面实现doBefore,doafter等方法
package com.zyd.aop.aspects;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
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;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Enumeration;
/**
* <strong>Order</strong> 定义切面执行的优先级,数字越低,优先级越高 <br>
* 在切入点之前执行:按order值有小到大的顺序执行 <br>
* 在切入点之后执行:按order值由大到小的顺序执行
*/
@Component
@Aspect
@Order(-5)
public class AppLogAspect {
private static final Logger LOGGER = LoggerFactory.getLogger(AppLogAspect.class);
// 保证每个线程都有一个单独的实例
private ThreadLocal<Long> threadLocal = new ThreadLocal<>();
@Pointcut("execution(* com.zyd.aop.controller.AopController.*(..))")
public void pointcut() {
}
@Before("pointcut()")
public void doBefore(JoinPoint joinPoint) {
threadLocal.set(System.currentTimeMillis());
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录请求的内容
LOGGER.info("Request URL: {}", request.getRequestURL().toString());
LOGGER.info("Request Method: {}", request.getMethod());
LOGGER.info("IP: {}", request.getRemoteAddr());
LOGGER.info("User-Agent:{}", request.getHeader("User-Agent"));
LOGGER.info("Class Method:{}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
LOGGER.info("Cookies:{}", request.getCookies());
LOGGER.info("Params:{}", Arrays.toString(joinPoint.getArgs()));
Enumeration<String> enums = request.getParameterNames();
while (enums.hasMoreElements()) {
String paraName = enums.nextElement();
LOGGER.info(paraName + ":" + request.getParameter(paraName));
}
}
@After("pointcut()")
public void doAfter(JoinPoint joinPoint) {
LOGGER.info("doAfter():{}", joinPoint.toString());
}
@AfterReturning("pointcut()")
public void doAfterReturning(JoinPoint joinPoint) {
LOGGER.info("耗时 :{}", ((System.currentTimeMillis() - threadLocal.get())) + "ms");
}
}
也可以加自己的注解,里面有值,可以在切面获取里面的值
@Component
@Aspect
@Order(-5)
public class BusinessLogAspect {
private static final Logger LOGGER = LoggerFactory.getLogger(BusinessLogAspect.class);
@Pointcut(value = "@annotation(com.zyd.aop.annotation.BusinessLogAnnotation)")
public void pointcut() {
}
@Before("pointcut()")
public void doBefore(JoinPoint point) throws NoSuchMethodException {
//获取拦截的方法名
Signature sig = point.getSignature();
MethodSignature msig = null;
if (!(sig instanceof MethodSignature)) {
LOGGER.error("该注解只能用于方法");
throw new IllegalArgumentException("该注解只能用于方法");
}
msig = (MethodSignature) sig;
Object target = point.getTarget();
//获取拦截方法的参数
Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
//获取操作业务的名称
BusinessLogAnnotation annotation = currentMethod.getAnnotation(BusinessLogAnnotation.class);
String bussinessName = annotation.value();
LOGGER.info("进入{}方法...", bussinessName);
}
}