• Spring AOP的增强处理


    就是@Before @Around @AfterReturning @AfterThrowing这几个标签的属性可以放到方法参数里面获取

    例子

    //正常操作
    @Around("service()")
    public void doAround(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()));
    }


    //获取annotation
    @Around("@annotation(log)") public Object around(ProceedingJoinPoint pjp, Log log) { //获取注解里的值 System.out.println("annotation around:" + log.description()); try { return pjp.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); return null; } }


    //获取异常
    @AfterThrowing(pointcut = "within(com.lgp.aop..*) && @annotation(log)", throwing = "ex")
    public void addAfterThrowingLogger(JoinPoint joinPoint, Log log, Exception ex) {
    logger.error("执行 " + log.description() + " 异常,error={}", ex.getMessage(), ex);
    }
     

    注意,参数名字应该一致。

  • 相关阅读:
    Openlayer 3 的画图测量面积
    Openlayer 3 的画线测量长度
    屏幕尺寸
    px和em,rem的区别
    水平和垂直居中
    Flex布局
    继承的几种方法及优缺点
    call ,apply 和 bind的用法与区别
    mybatis springmvc velocity的demo
    正则同时包含两个关键字
  • 原文地址:https://www.cnblogs.com/ydymz/p/9542064.html
Copyright © 2020-2023  润新知