AOP+自定义注解

配置依赖:

    compile('org.springframework.boot:spring-boot-starter-aop')

方法1——通用注解:

@Aspect
@Component
public class AuditAspect {
    //@Pointcut("execution(public * com.example.controller.*.*(..))")

    @Pointcut("execution(public * com.example.controller.CustomerSwaggerController.*(..))")
    public void webLog(){}

    @Before("webLog()")
    public void deBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 记录下请求内容
        System.out.println("URL : " + request.getRequestURL().toString());
        System.out.println("HTTP_METHOD : " + request.getMethod());
        System.out.println("IP : " + request.getRemoteAddr());
        System.out.println("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        System.out.println("ARGS : " + Arrays.toString(joinPoint.getArgs()));

    }


    @AfterReturning(returning = "ret", pointcut = "webLog()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 处理完请求,返回内容
        System.out.println("方法的返回值 : " + ret);
    }

    //后置异常通知
    @AfterThrowing("webLog()")
    public void throwss(JoinPoint jp){
        System.out.println("方法异常时执行.....");
    }

    //后置最终通知,final增强,不管是抛出异常或者正常退出都会执行
    @After("webLog()")
    public void after(JoinPoint jp){
        System.out.println("方法最后执行.....");
    }

    //环绕通知,环绕增强,相当于MethodInterceptor
    @Around("webLog()")
    public Object arround(ProceedingJoinPoint pjp) {
        System.out.println("方法环绕start.....");
        try {
            Object o =  pjp.proceed();
            System.out.println("方法环绕proceed,结果是 :" + o);
            return o;
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
    }
}


方法2——自定义注解:

新建注解:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface UserAccess {
    String desc() default "无信息";
}

实现Aspect:

获取方法和类的注解内容

@Component
@Aspect
public class UserAccessAspect {
    

    @Around("@annotation(usersAccess)")
    public Object around(ProceedingJoinPoint pjp, UserAccess usersAccess) {
        //得到类注解
        Class<?> cls = pjp.getTarget().getClass();
        RequestMapping classAnnotation = cls.getAnnotation(RequestMapping.class);
        String apiName = classAnnotation.value()[0];

        //获取方法注解
        MethodSignature s = (MethodSignature) pjp.getSignature();
        RequestMapping methodAnnotation = s.getMethod().getAnnotation(RequestMapping.class);
        String methodName = methodAnnotation.value()[0];

        //记录开始时间
        long startTime = System.currentTimeMillis();

        System.out.println("hello around start:" + usersAccess.desc());
        try {
            return pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return null;
        } finally {

            System.out.println("hello around end apiName:" + apiName);
            System.out.println("hello around end methodName:" + methodName);

            //记录结束时间
            long endTime = System.currentTimeMillis();
            System.out.println("time span" + (endTime - startTime));
        }
    }
}



猜你喜欢

转载自blog.csdn.net/jonwu0102/article/details/80937160