aop log切面
@Aspect:描述一个切面类,定义切面类的时候需要打上这个注解
@Component:spring-boot配置类
package com.*.*.tools; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; 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; @Aspect @Component public class LogAspect { /** * 功能描述: 拦截对这个包下所有方法的访问 * * @param:[] * @return:void **/ @Pointcut("execution(* com.*.*.controller.*..*(..))") public void loginLog() { } // 前置通知 @Before("loginLog()") public void loginBefore(JoinPoint joinPoint) { // 我们从请求的上下文中获取request,记录请求的内容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); System.out.println("请求路径 : " + request.getRequestURL()); System.out.println("请求方式 : " + request.getMethod()); System.out.println("方法名 : " + joinPoint.getSignature().getName()); System.out.println("类路径 : " + joinPoint.getSignature().getDeclaringTypeName()); System.out.println("参数 : " + Arrays.toString(joinPoint.getArgs())); } // @AfterReturning(returning = "object", pointcut = "loginLog()") // public void doAfterReturning(Object object) { // // System.out.println("方法的返回值 : " + object); // } // 方法发生异常时执行该方法 @AfterThrowing(throwing = "e",pointcut = "loginLog()") public void throwsExecute(JoinPoint joinPoint, Exception e) { System.err.println("方法执行异常 : " + e.getMessage()); } // 后置通知 // @After("loginLog()") // public void afterInform() { // // System.out.println("后置通知结束"); // } // 环绕通知 @Around("loginLog()") public Object surroundInform(ProceedingJoinPoint proceedingJoinPoint) { long startTime=System.currentTimeMillis(); try { Object o = proceedingJoinPoint.proceed(); long times=System.currentTimeMillis()-startTime; MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature(); String methodName = signature.getDeclaringTypeName() + "." + signature.getName(); System.out.println("【"+methodName+"】执行时间:" +times/1000+"s" ); return o; } catch (Throwable e) { e.printStackTrace(); return null; } } }