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;
    } 
  • 相关阅读:
    JAVA中handleEvent和action的区别
    Hessian的使用以及理解
    Java基础中的RMI介绍与使用
    Callable与Runable接口 submit与execute区别
    XXL-JOB原理--定时任务框架简介(一)
    11.并发包阻塞队列之LinkedBlockingQueue
    并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法
    正确实现用spring扫描自定义的annotation
    自贡进入“刷脸卡”时代 人脸识别支付“黑科技”现身自流井老街
    谷歌最新研究:量子计算机能在8小时内破解2048位RSA加密
  • 原文地址:https://www.cnblogs.com/mkl7/p/10697219.html
Copyright © 2011-2022 走看看