**
写在开头!
**
正常情况下, 基于注解的声明要优先于基于 XML 的声明.
通过 AspectJ 注解, 切面可以与 AspectJ 兼容, 而基于 XML 的配置则是 Spring 专有的.
由于 AspectJ 得到越来越多的 AOP 框架支持, 所以以注解风格编写的切面将会有更多重用的机会.
基于 XML ---- 声明切面
当使用 XML 声明切面时, 需要在 <beans> 根元素中导入 aop Schema
在 Bean 配置文件中, 所有的 Spring AOP 配置都必须定义在 <aop:config> 元素内部.
对于每个切面而言, 都要创建一个 <aop:aspect> 元素来为具体的切面实现引用后端 Bean 实例. (相当于注解 @Aspect 标注在Bean上)
切面 Bean 必须有一个标示符(也就是 bean id), 供 <aop:aspect> 元素引用
<!-- 配置切面的 bean. -->
<bean id="loggingAspect"
class="com.atguigu.spring.aop.xml.LoggingAspect"></bean>
<!-- 配置 AOP -->
<aop:config>
<!-- 配置切面 -->
<aop:aspect ref="loggingAspect"></aop:aspect>
</aop:config>
基于 XML ---- 声明切入点
切入点使用 <aop:pointcut> 元素声明
切入点必须定义在<aop:config>或者<aop:aspect>元素下.
定义在 <aop:config> 元素下: 对所有切面都有效
定义在 <aop:aspect> 元素下: 只对当前切面有效
基于 XML 的 AOP 配置不允许在切入点表达式中用名称引用其他切入点.
<!-- 配置切面的 bean. -->
<bean id="loggingAspect"
class="com.atguigu.spring.aop.xml.LoggingAspect"></bean>
<!-- 配置 AOP -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut id="pointcut" expression="execution(* com.atguigu.spring.aop.xml.ArithmeticCalculator.*(int, int))" />
<!-- 配置切面 -->
<aop:aspect ref="loggingAspect"></aop:aspect>
</aop:config>
基于 XML ---- 声明通知
在 aop Schema 中, 每种通知类型都对应一个特定的 XML 元素:
<aop:before>
<aop:after>
<aop:after-throwing>
<aop:after-returning>
<aop:around>
通知元素需要使用 pointcut-ref属性 来引用切入点, 或用 pointcut属性 直接嵌入切入点表达式.
method属性 指定切面类中通知方法的名称.
<!-- 配置切面的 bean. -->
<bean id="loggingAspect"
class="com.atguigu.spring.aop.xml.LoggingAspect"></bean>
<!-- 配置 AOP -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut id="pointcut" expression="execution(* com.atguigu.spring.aop.xml.ArithmeticCalculator.*(int, int))" />
<!-- 配置切面 -->
<aop:aspect ref="loggingAspect">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
<aop:around method="aroundMethod" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
基于 XML ---- 切面优先级
在使用多个切面的情况下,在标签<aop:aspect>中使用属性order来为切面进行优先级排序
<!-- 配置切面的 bean. -->
<bean id="loggingAspect"
class="com.atguigu.spring.aop.xml.LoggingAspect"></bean>
<bean id="vlidationAspect"
class="com.atguigu.spring.aop.xml.VlidationAspect"></bean>
<!-- 配置 AOP -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(* com.atguigu.spring.aop.xml.ArithmeticCalculator.*(int, int))"
id="pointcut"/>
<!-- 配置切面及通知 -->
<aop:aspect ref="loggingAspect" order="2">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
<aop:around method="aroundMethod" pointcut-ref="pointcut"/>
</aop:aspect>
<aop:aspect ref="vlidationAspect" order="1">
<aop:before method="validateArgs" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>