Spring实战(十一) 在Spring XML中配置AOP

  如果你没有源码,不能为通知类添加注解,又不想将AspectJ注解放入到你的代码中,必须选择XML配置了。

  1、Spring XML配置文件

  解析参考:http://www.cnblogs.com/bigbigbigo/articles/8375530.html

 

  2、AOP配置元素

aop配置元素 描述
<aop:advisor> 定义aop通知器
<aop:after> 定义aop后置通知(不管被通知的方法是否执行成功)
<aop:after-returning> 定义aop after-returning通知
<aop:after-throwing> 定义aop after-throwing通知
<aop:around> 定义aop环绕通知
<aop:aspect> 定义切面
<aop:aspect-autoproxy> 启动@AspectJ注解驱动的切面
<aop:before> 定义aop前置通知
<aop:config> 顶层的aop配置元素,大多数的<aop:*>元素必须包含在<aop:config>内
<aop:declare-parents> 为被通知的 对象引入的额外的接口,并透明地实现
<aop:pointcut> 定义切点

  

  3、声明前置通知和后置通知

    <aop:config>
        <aop:aspect ref="audience">
            <aop:before 
                    pointcut="execution(** concert.Performance.perform(..))"
                    method="silenceCellPhone"/>
            
            <aop:before
                    pointcut="execution(** concert.Performance.perform(..))"
                    method="takeSeats"/>

            <aop:before
                    pointcut="execution(** concert.Performance.perform(..))"
                    method="applause"/>

            <aop:before
                    pointcut="execution(** concert.Performance.perform(..))"
                    method="demandRefund"/>
           
        </aop:aspect>
    </aop:config>

   使用<aop:pointcut>元素更简洁:

<aop:config>
        <aop:aspect ref="audience">
            <aop:pointcut id="performance" expression="execution(** concert.Performance.perform(..))"/>
<aop:before pointcut-ref="performance" method="silenceCellPhones"/> <aop:before pointcut-ref="performance" method="takeSeats"/> <aop:before pointcut-ref="performance" method="applause"/> <aop:before pointcut-ref="performance" method="demandRefund"/> </aop:aspect> </aop:config>

 

  

  4、声明环绕通知

    <aop:config>
      
        <!--声明环绕通知-->
        <aop:aspect ref="audienceAround">
            <aop:pointcut id="performance" expression="execution(** concert.Performance.perform(..))"/>
                          <aop:around pointcut-ref="performance" method="watchPerformance"/>
        </aop:aspect>
        
    </aop:config>

 

 

  5、为通知传递参数

  假设方法perform(int numbers)有一个int型参数,而这个参数会传递到通知方法中。

  因为在XML中,“&”符号会被解析为实体的开始,所以我们用“and”关键字代替“&&”。

<aop:config>
        <aop:aspect ref="audience">
            <aop:pointcut id="performance" expression="execution(** concert.Performance.perform(int)) and args(numbers)"/>

            <aop:before
                    pointcut-ref="performance"
                    method="demandRefund"/>

        </aop:aspect>
    </aop:config>

 

 

  6、为对象引入新的方法(通过切面引入新的功能)

  之前通过使用@DeclareParents注解为被通知的对象引入新方法,但AOP因为不是AspecJ特有。

  在XML中使用<aop:declare-parents>,可以实现相同的功能。

<aop:aspect>
       <aop:declare-parents types-matching="concert.Performance+" 
                            implement-interface="concert.Encoreable"
                            default-impl="concert.DefaultEncorable"/>
</aop:aspect>

  这里使用default-impl属性来显示指定Encoreable的实现。

  我们还可以使用delegate-ref属性来引用一个Spring bean作为引入的委托,这需要在Spring上下文中存在一个ID为encorableDelegate的bean:

<aop:aspect>
       <aop:declare-parents types-matching="concert.Performance+" 
                            implement-interface="concert.Encoreable"
                            default-ref="DefaultEncorable"/>
</aop:aspect>

 

  使用default-impl,叫直接标识委托;

  使用delegate-ref,叫引用一个bean作为引入的委托

 

 

posted @ 2018-01-29 05:28  爆炸的果核  阅读(464)  评论(0编辑  收藏  举报