基于XML的方式配置AOP

第一步:把通知类用 bean 标签配置起来

<bean id="txManager" class="com.atguigu.account.utils.TransactionManager">
    <!-- 注入ConnectionUtils -->
    <property name="connectionUtils" ref="connectionUtils" />
</bean>

第二步:使用 aop:config 声明 aop 配置

  aop:config: 
        作用:用于声明开始 aop 的配置  
  <aop:config> 
     
  </aop:config>

第三步:在使用 aop:config标签里面配置 aop:aspect切面

  aop:aspect: 
     作用:   用于配置切面。  属性:   
     id:给切面提供一个唯一标识。   
     ref:引用配置好的通知类 bean 的 id。 
    <aop:aspect id="txAdvice" ref="txManager"> 
          <!--配置通知的类型要写在此处--> 
    </aop:aspect>

第四步:使用 aop:pointcut 配置切入点表达式

  aop:pointcut:  
        作用:   用于配置切入点表达式。就是指定对哪些类的哪些方法进行增强。  
        属性:   expression:用于定义切入点表达式。   
        id:用于给切入点表达式提供一个唯一标识 
  <!--配置通用切入点表达式,需要将该标签放置在通知之前-->
  <aop:pointcut id="pt1" expression="execution(* com.atguigu.account.service.impl.*.*(..))" />

第五步:使用 aop:xxx 配置对应的通知类型

<aop:config>
    <!--配置通用切入点表达式-->
    <aop:pointcut id="pt1" expression="execution(* com.atguigu.account.service.impl.*.*(..))" />
    <aop:aspect id="txAdvice" ref="txManager">
        <!--配置前置通知:开启事务-->
        <aop:before method="beginTransaction" pointcut-ref="pt1" />
        <!--配置后置通知:提交事务-->
        <aop:after-returning method="commit" pointcut-ref="pt1" />
        <!--配置异常通知:回滚事务-->
        <aop:after-throwing method="rollback" pointcut-ref="pt1" />
        <!--配置最终通知:释放连接-->
        <aop:after method="release" pointcut-ref="pt1" />
    </aop:aspect>
</aop:config>

六、详细解析

  aop:before 
   作用:   用于配置前置通知。指定增强的方法在切入点方法之前执行   
   属性:  
        method:用于指定通知类中的增强方法名称   
        ponitcut-ref:用于指定切入点的表达式的引用   
        poinitcut:用于指定切入点表达式   
        执行时间点:切入点方法执行之前执行 
   <aop:before method="beginTransaction" pointcut-ref="pt1"/> 


    aop:after-returning 
        作用:   用于配置后置通知  属性:   
        method:指定通知中方法的名称。   
        pointct:定义切入点表达式   
        pointcut-ref:指定切入点表达式的引用  
        执行时间点:   切入点方法正常执行之后。它和异常通知只能有一个执行 
    <aop:after-returning method="commit" pointcut-ref="pt1"/> 


    aop:after-throwing 
         作用:   用于配置异常通知  属性:   
         method:指定通知中方法的名称。   
         pointct:定义切入点表达式   
         pointcut-ref:指定切入点表达式的引用  
         执行时间点:   切入点方法执行产生异常后执行。它和后置通知只能执行一个 
   <aop:after-throwing method="rollback" pointcut-ref="pt1"/>


   aop:after 
         作用:   用于配置最终通知  属性:   
         method:指定通知中方法的名称。   
         pointct:定义切入点表达式   
         pointcut-ref:指定切入点表达式的引用  
         执行时间点:   无论切入点方法执行时是否有异常,它都会在其后面执行。 
   <aop:after method="release" pointcut-ref="pt1"/> 



  aop:around:  
         作用:   用于配置环绕通知  
         属性:   
              method:指定通知中方法的名称。   
              pointct:定义切入点表达式   
              pointcut-ref:指定切入点表达式的引用  
         说明:   它是 spring 框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式。  
         注意:   通常情况下,环绕通知都是独立使用的 

七、环绕通知

  /** 
   * 环绕通知  * @param pjp 
   *  spring 框架为我们提供了一个接口:ProceedingJoinPoint,它可以作为环绕通知的方法参数。  
   *  在环绕通知执行时,spring 框架会为我们提供该接口的实现类对象,我们直接使用就行。  
   * @return  
   */ 
   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; 
     }
posted @ 2020-07-13 20:19  jock_javaEE  阅读(126)  评论(0编辑  收藏  举报