zoukankan      html  css  js  c++  java
  • 通知类型 重点: 环绕通知 (XML配置)

    前置通知:在切入点方法执行之前执行  <aop:before method="" pointcut-ref="" ></aop:before>

    后置通知:在切入点方法正常执行之后值。它和异常通知永远只能执行一个  <aop:after-returning method="" pointcut-ref="" ></aop:before>

    异常通知:在切入点方法执行产生异常之后执行。它和后置通知永远只能执行一个  <aop:after-throwing method="" pointcut-ref="" ></aop:before>

    最终通知:无论切入点方法是否正常执行它都会在其后面执行  <aop:after method="" pointcut-ref="" ></aop:before>

    配置切入点表达式

      id属性    用于指定表达式的唯一标识。

      expression属性    用于指定表达式内容               

      此标签写在aop:aspect标签内部只能当前切面使用。               

      它还可以写在aop:aspect外面,此时就变成了所有切面可用

    环绕通知

    配置方式: 

    <!-- 配置通知bean --> 
    <bean id="txManager" class="com.itheima.utils.TransactionManager"> 
      <property name="dbAssit" ref="dbAssit"></property> 
    </bean>
    <aop:config> 
      <aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt1"/>
      <aop:aspect id="txAdvice" ref="txManager">
        <!-- 配置环绕通知 -->
        <aop:around method="transactionAround" pointcut-ref="pt1"/>
      </aop:aspect>
    </aop:config>

    作用: 用于配置环绕通知

    属性:

      method:指定通知中方法的名称。

      pointct:定义切入点表达式

      pointcut-ref:指定切入点表达式的引用

    说明:

      它是spring框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式。

    注意:

      通常情况下,环绕通知都是独立使用的

    Spring框架为我们提供了一个接口:ProceedingJoinPoint

      该接口有一个方法proceed()此方法就相当于明确调用切入点方法。

      该接口可以作为环绕通知的方法参数,在程序执行时,spring框架会为我们提供该接口的实现类供我们使用。

    public Object transactionAround(ProceedingJoinPoint pjp) { 
            //定义返回值 
            Object rtValue = null;
             try { 
                //获取方法执行所需的参数
                 Object[] args = pjp.getArgs(); 
                 //前置通知:开启事务
                 beginTransaction(); 
                 //执行方法
                 rtValue = pjp.proceed(args);
                 //后置通知:提交事务 
                 commit(); 
             }catch(Throwable e) {
                  //异常通知:回滚事务
                  rollback();
                  e.printStackTrace(); 
             }finally { 
                  //最终通知:释放资源
                  release(); 
             } 
             return rtValue;
    } 
  • 相关阅读:
    Qt计算器开发(三):执行效果及项目总结
    [HNOI2019]校园旅行
    How to fix nuget Unrecognized license type MIT when pack
    How to fix nuget Unrecognized license type MIT when pack
    git 通过 SublimeMerge 处理冲突
    git 通过 SublimeMerge 处理冲突
    git 上传当前分支
    git 上传当前分支
    gif 格式
    gif 格式
  • 原文地址:https://www.cnblogs.com/mkl7/p/10697219.html
Copyright © 2011-2022 走看看