zoukankan      html  css  js  c++  java
  • 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作为引入的委托

     

  • 相关阅读:
    在Linux下运行YY,WINE方式,主要注册表修改点及字体文件列表
    安卓so下,cmake编译系统,如何仅导出指定符号
    AutoHotKey脚本模板:初始化、配置、退出
    资源ID管理插件:VS6/VS.NET
    ListView控件,表格模式下,如何调整行高
    通过wscript运行的JS脚本,如何引入另一个JS文件
    64位编译器下,将指针转换成UINT32,不需要修改编译选项的编码方式
    视频帧双缓冲区的两个版本
    opencv、numpy中矩阵转置,矩阵内的固定位置相应的坐标变换
    CMake下,某些选项的后调整
  • 原文地址:https://www.cnblogs.com/bigbigbigo/p/8375651.html
Copyright © 2011-2022 走看看