zoukankan      html  css  js  c++  java
  • 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);
    }
     

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

  • 相关阅读:
    Mysql 之根据经纬度按距离排序
    Python的列表和元组
    go实现堆排序、快速排序、桶排序算法
    微信Hook劫获protobuf数据
    手机号批量查询微信昵称/网名/名称
    保存整个网页的内容
    天地图官网引入文件
    Postman-动态传参
    JAVA FileOutputStream与BufferedOutputStream的区别
    JAVA中sleep()和wait()的区别
  • 原文地址:https://www.cnblogs.com/ydymz/p/9542064.html
Copyright © 2011-2022 走看看