zoukankan      html  css  js  c++  java
  • 基于XML的AOP配置(2)-环绕通知

    配置方式:
    <aop:config>
        <aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))"
      id="pt1"/>
      <aop:aspect id="txAdvice" ref="txManager">
        <!-- 配置环绕通知 -->
        <aop:around method="aroundPrint" pointcut-ref="pt1"/>
      </aop:aspect>
    </aop:config>
    aop:around:
    作用:
    用于配置环绕通知
    属性:
    method:指定通知中方法的名称。
    pointct:定义切入点表达式
    pointcut-ref:指定切入点表达式的引用
    说明:
    它是 spring 框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式。
    注意:
    通常情况下,环绕通知都是独立使用的
    /**
         * 环绕通知
         * 问题:
         *     当我们配置了环绕通知后,切入点方法不执行,而通知方法执行了
         * 分析:
         *     通过对比动态代理中的环绕通知代码,发现动态代理的环绕通知有明确的切入点方法调用而我们的代码中没有
         * 解决:
         *     Spring框架为我们提供ProceedingJoinPoint。该接口有一个方法proceed(),此方法就相当于明确调用切入点方法
         *     该接口可以作为环绕通知的方法参数,在程序执行时,spring框架会为我们提供该接口的实现类供我们使用
         * */
        public Object aroundPrint(ProceedingJoinPoint pjp){
            try {
                Object args[] = pjp.getArgs();//得到方法执行所需要的参数
                System.out.println("环绕通知logger开始记录日志了前置");
                Object returnValue = pjp.proceed();//明确调用业务层(切入点方法)
                System.out.println("环绕通知logger开始记录日志了后置");
                return returnValue;
            } catch (Throwable throwable) {
                System.out.println("环绕通知logger开始记录日志了异常");
                throw new RuntimeException(throwable);
            }finally {
                System.out.println("环绕通知logger开始记录日志了最终");
            }
        }

  • 相关阅读:
    人生如此
    微软十七道智力面试题及答案
    【Flink系列十】Flink作业提交过程的调试和诊断
    【Flink系列九】Flink 作业提交遇到的问题记录以及原理
    Jackson ObjectMapper JSON序列化工具使用笔记,由浅入深
    既有设计模式的lambda重构
    观察者模式/Observer
    函数式接口java.util.function
    面向对象世界的七个设计原则
    重构-改善既有代码设计读后灵光
  • 原文地址:https://www.cnblogs.com/qzhc/p/11971772.html
Copyright © 2011-2022 走看看