86 lines
2.9 KiB
Java

package com.jsl.oa.aspect;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.jetbrains.annotations.NotNull;
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;
/**
* 业务切面
* <hr/>
* 对业务进行切入,进行对内容操作的预处理
*
* @since v1.2.0
* @version v1.2.0
* @author xiao_lfeng
*/
@Slf4j
@Aspect
@Component
@RequiredArgsConstructor
public class BusinessAop {
/**
* 在控制器的所有方法执行前执行
*
* @param joinPoint 切入点提供对方法执行的信息
*/
@Before("execution(* com.jsl.oa.controllers.*.*(..))")
public void beforeController(@NotNull JoinPoint joinPoint) {
// 从ServletRequest中获取用户信息
ServletRequestAttributes servletRequestAttributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (servletRequestAttributes != null) {
HttpServletRequest request = servletRequestAttributes.getRequest();
// 获取方法签名
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
String methodName = methodSignature.getName();
log.info("[CONTROL] 执行 {} 接口 | 地址: [{}]{}", methodName, request.getMethod(), request.getServletPath());
} else {
throw new RuntimeException("无法获取信息");
}
}
/**
* 在服务的所有方法执行前执行
*
* @param joinPoint 切入点提供对方法执行的信息
*/
@Before("execution(* com.jsl.oa.services.impl.*.*(..))")
public void beforeService(@NotNull JoinPoint joinPoint) {
// 获取方法签名
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
String methodName = methodSignature.getName();
log.info("[SERVICE] 执行 {} 业务", methodName);
}
/**
* 在DAO的所有方法执行前执行
*
* @param joinPoint 切入点提供对方法执行的信息
*/
@Before("execution(* com.jsl.oa.dao.*.*(..))")
public void beforeDao(@NotNull JoinPoint joinPoint) {
// 获取方法签名
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
String methodName = methodSignature.getName();
Object[] args = joinPoint.getArgs();
log.info("[DAO] 操作 {} 记录", methodName);
if (args.length != 0) {
log.debug("\t> 传入信息:{}", Arrays.toString(args));
}
}
}