AspectJ是一种面向切面编程(AOP)的框架,它可以在不修改原有代码的情况下,为其添加额外的行为。AspectJ使用三个核心心心概念:连接点(Join Point),切面(Aspect),和建议(Advice)
该类使用AspectJ注解来实现日志记录
连接点(Join Point)是指需要被切面影响的方法或构造器。您可以使用pointcut语句来定义连接点
java@Pointcut("execution(public * com.test.controller..*.*(..))")
public void webLog() {}
切面(Aspect)是指包含pointcut和建议的类。您可以使用@Aspect注解来标记一个类为一个切面,例如:
java@Aspect
@Component
public class WebLogAcpect {
// ...
}
建议(Advice)是指在连接点之前或之后执行的方法。您可以使用@Before, @After, @Around等注解来标记一个方法为一个建议,例如:
java
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// ...
}
java
@Aspect
@Component
public class WebLogAcpect {
private Map<Long, Map<String, List<Long>>> threadMap = new ConcurrentHashMap<>(50);
private Logger logger = LoggerFactory.getLogger(WebLogAcpect.class);
@Pointcut("execution(public * com.test.controller..*.*(..))")
public void webLog() {}
// @Pointcut("execution(public * com.adverseEvents.restController..*.*(..))")
public void webLog1() {}
/**
* 前置通知:在连接点之前执行的通知
*
* @param joinPoint
* @throws Throwable
*/
// @Before("webLog() || webLog1()")
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
logger.info("=============Before========================");
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()));
Map<String, List<Long>> methodTimeMap = threadMap.get(Thread.currentThread().getId());
List<Long> list;
if (methodTimeMap == null) {
methodTimeMap = new HashMap<>();
list = new LinkedList<>();
list.add(System.currentTimeMillis());
methodTimeMap.put(joinPoint.toShortString(), list);
threadMap.put(Thread.currentThread().getId(), methodTimeMap);
} else {
list = methodTimeMap.get(joinPoint.toShortString());
if (list == null) {
list = new LinkedList<>();
}
list.add(System.currentTimeMillis());
methodTimeMap.put(joinPoint.toShortString(), list);
}
}
// @AfterReturning(returning = "ret", pointcut = "webLog() || webLog1()")
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
// 处理完请求,返回内容
if (ret == null) {
logger.info("RESPONSE:null");
logger.info("=============After========================");
return;
}
logger.info("RESPONSE : " + ret.toString());
logger.info("=============After========================");
}
// @After(value = "webLog() || webLog1()")
@After(value = "webLog()")
public void after(JoinPoint joinPoint) {
Map<String, List<Long>> methodTimeMap = threadMap.get(Thread.currentThread().getId());
List<Long> list = methodTimeMap.get(joinPoint.toShortString());
System.out.println("耗时:" + (System.currentTimeMillis() - list.get(list.size() - 1)));
list.remove(list.size() - 1);
}
}
本文作者:Weee
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!