package com.after90s.frame.aspectj;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import com.after90s.common.annotation.Log;
import com.after90s.common.utils.ShiroUtils;
import com.after90s.project.web.user.entity.UserEntity;
@Aspect
@Component
@EnableAsync
public class LogAspectJ {
@Pointcut("@annotation(com.after90s.common.annotation.Log)")
public void logCutPoint() {
}
@Before("logCutPoint()")
public void beforeAdvice() {
System.out.println("======================方法执行前===================");
}
@After("logCutPoint()")
public void afterAdvice() {
System.out.println("======================方法执行后===================");
}
@AfterReturning(pointcut = "logCutPoint()", returning = "joinPoint")
public void afterReturningAdvice(JoinPoint joinPoint) {
handleLog(joinPoint, null);
}
@AfterThrowing(value = "logCutPoint()", throwing = "e")
public void doAfter(JoinPoint joinPoint, Exception e) {
handleLog(joinPoint, e);
}
@Async
private void handleLog(final JoinPoint joinPoint, final Exception e) {
Log controllerLog = getMyAnnotationLog(joinPoint);
if (controllerLog == null) {
return;
}
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
if (currentUser != null) {
System.out
.println("==================" "操作:" + className + "." + methodName);
}
if (e != null) {
System.out.println("===================" + e.getMessage() + "操作:" + className + "." + methodName);
}
}
private Log getMyAnnotationLog(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null && method.getAnnotation(Log.class) != null) {
return method.getAnnotation(Log.class);
}
return null;
}
}