Spring提供了4种类型的AOP支持:

  • 基于代理的经典Spring AOP;
  • 纯POJO切面;
  • @AspectJ注解驱动的切面;
  • 注入式AspectJ切面(适用于Spring各版本)。

Spring AOP构建在动态代理基础之上,因此,Spring对AOP的支持局限于方法拦截。

引入了简单的声明式AOP和基于注解的AOP之后,Spring经典的AOP看起来就显得非常笨重和过于复杂,直接使用 ProxyFactory Bean会让人感觉厌烦

Spring借鉴了AspectJ的切面,以提供注解驱动的AOP

方法拦截可以满足绝大部分的需求。如果需要方法拦截之外的连接点拦截功能,那么我们可以利用Aspect来补充Spring AOP的功能。

在Spring AOP中,要使用AspectJ的切点表达式语言来定义切点

《AspectJ in Action》第二版(Manning,2009,www.manning.com/laddad2/)。

<aop:aspectj-autoproxy>元素,它能够自动代理AspectJ注解的通知类

如下的XML代码片段与之前基于AspectJ的引入功能是相同:

1
2
3
4
5
6
7
<aop:aspect>
<aop:declare-parents
types-matching="concert.Performance+"
implement-interface="concert.Encoreable"
default-impl="concert.DefaultEncoreable"
/>
</aop:aspect>

<aop:declare-parents>声明了此切面所通知的bean要在它的对象层次结构中拥有新的父类型。具体到本例中,类型匹配Performance接口(由types-matching属性指定)的那些bean在父类结构中会增加Encoreable接口(由implement-interface属性指定)