方法一:
package com.live.log.aspect;
public enum AnnotationTypeEnum {
REQUEST_MAPPING("RequestMapping"),
POST_MAPPING("PostMapping"),
GET_MAPPING("GetMapping"),
PUT_MAPPING("PutMapping"),
DELETE_MAPPING("DeleteMapping"),
PATCH_MAPPING("PatchMapping");
AnnotationTypeEnum(String packagePath) {
this.packagePath = packagePath;
}
private String packagePath;
public String getPackagePath() {
return packagePath;
}
}
package com.live.log.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import static com.live.log.aspect.AnnotationTypeEnum.REQUEST_MAPPING;
@Aspect
@Component
public class LiveLogAspect {
@Pointcut(value = "execution(public * com.live.controller.*.*(..))") // 切点
private void controllerPointCut() {
}
@Around(value = "controllerPointCut()")
private Object Around(ProceedingJoinPoint proceedingJoinPoint) {
Long startTime = System.currentTimeMillis();// 步入时间戳
Object proceed = null;
try {
MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
Method method = proceedingJoinPoint.getTarget().getClass().getMethod(signature.getName(), signature.getParameterTypes());
String requestURL = getClassAnnotationPath(proceedingJoinPoint.getTarget().getClass()) + getMethodAnnotationPath(method);
System.out.println("requestURL:" + requestURL);
proceed = proceedingJoinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println(System.currentTimeMillis() - startTime + "ms");
return proceed;
}
@AfterThrowing(value = "controllerPointCut()")
private void AfterThrowing() {
System.out.println("异常通知");
}
private static String getMethodAnnotationPath(Method method) {
String methodPath = "";
Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
for (Annotation annotation : declaredAnnotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
if (REQUEST_MAPPING.getPackagePath().equals(annotationType.getSimpleName())) {
RequestMapping requestMapping = method.getDeclaredAnnotation(RequestMapping.class);
String[] value = requestMapping.value();
if (value.length != 0) {
methodPath = value[0];
}
}
}
return methodPath;
}
private static <T> String getClassAnnotationPath (Class<T> targetClass) {
Annotation[] declaredAnnotations = targetClass.getDeclaredAnnotations();
String path = "";
for (Annotation annotation : declaredAnnotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
if (REQUEST_MAPPING.getPackagePath().equals(annotationType.getSimpleName())) {
RequestMapping requestMapping = targetClass.getDeclaredAnnotation(RequestMapping.class);
String[] value = requestMapping.value();
if (value.length != 0) {
path = value[0];
}
}
}
return path;
}
}
方法二:
package com.live.log.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import static com.live.log.aspect.AnnotationTypeEnum.REQUEST_MAPPING;
@Aspect
@Component
public class LiveLogAspect {
@Pointcut(value = "execution(public * com.live.controller.*.*(..))") // 切点
private void controllerPointCut() {
}
@Around(value = "controllerPointCut()")
private Object Around(ProceedingJoinPoint proceedingJoinPoint) {
Long startTime = System.currentTimeMillis();// 步入时间戳
Object proceed = null;
try {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String requestURL = request.getRequestURL().toString();
System.out.println("method:" + request.getMethod());
System.out.println("requestURL:" + requestURL);
proceed = proceedingJoinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println(System.currentTimeMillis() - startTime + "ms");
return proceed;
}
@AfterThrowing(value = "controllerPointCut()")
private void AfterThrowing() {
System.out.println("异常通知");
}
笔者在此推荐方法二。