如果你没有源码,不能为通知类添加注解,又不想将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作为引入的委托

最新文章

  1. Write Your software base on plugin(C/C++ ABI)
  2. Entity Framework学习 - 3.关联查询
  3. nginx模块开发(18)—日志分析
  4. andeoid学习笔记七
  5. 按钮控件Button
  6. linux下curl编程
  7. Android在子线程中更新UI(二)
  8. Spring入门学习(二)三种实例化bean的方法
  9. 基于Windows服务的聊天程序(一)
  10. Windows搭建golang开发平台
  11. php应用pack函数转unicode为utf8
  12. 3、MHC主要组织相容性复合体
  13. IQ调制原理
  14. win7重装系统笔记
  15. 【详解】JNI(Java Native Interface)(一)
  16. Codeforces Round #506 D. Concatenated Multiples题解
  17. [py]python中__new__作用
  18. BZOJ2209: [Jsoi2011]括号序列
  19. JDK中ClassLoader的分类以及ClassLoader间的层次关系
  20. Free GIS Software

热门文章

  1. redis常见7种使用场景
  2. MAC下STF安装及踩坑
  3. ffmpeg编码h264设置规格
  4. 简易总结react-hook三大基础
  5. P4127 [AHOI2009]同类分布
  6. centos7 安装 ftp 服务及创建 repo源
  7. redis多实例与主从同步及高级特性(数据过期机制,持久化存储)
  8. 012-数据结构-树形结构-哈希树[hashtree]、字典树[trietree]、后缀树
  9. AIX系统命令
  10. 安装mysql-proxy