Spring-AOP:基于配置文件方式的AOP
2016-08-12 10:48
337 查看
除了使用 AspectJ 注解声明切面, Spring 也支持在 Bean 配置文件中声明切面. 这种声明是通过 aop schema 中的 XML 元素完成的.
正常情况下, 基于注解的声明要优先于基于 XML 的声明. 通过 AspectJ 注解, 切面可以与 AspectJ 兼容, 而基于 XML 的配置则是 Spring 专有的. 由于 AspectJ 得到越来越多的 AOP 框架支持, 所以以注解风格编写的切面将会有更多重用的机会.
当使用 XML 声明切面时, 需要在 <beans> 根元素中导入 aop Schema
在 Bean 配置文件中, 所有的 Spring AOP 配置都必须定义在 <aop:config> 元素内部. 对于每个切面而言, 都要创建一个
<aop:aspect> 元素来为具体的切面实现引用后端 Bean 实例.
切面 Bean 必须有一个标示符, 供 <aop:aspect> 元素引用
声明切面
在声明切面之前,需要先为切面类声明一个普通bean。然后在<aop:config>中使用<aop:aspect> 声明为一个切面:
<bean id="arithmeticCalculator"
class="com.atguigu.spring.aop.xml.ArithmeticCalculatorImpl"></bean>
<bean id="loggingAspect"
class="com.atguigu.spring.aop.xml.LoggingAspect"></bean>
<bean id="validatingAspect"
class="com.atguigu.spring.aop.xml.ValidatingAspect"></bean>
<!-- 配置AOP -->
<aop:config>
<!-- 配置切面 -->
<aop:aspect ref="loggingAspect" order="2">
</aop:aspect>
<aop:aspect ref="validatingAspect" order="1">
</aop:aspect>
</aop:config>
在声明切面的时候也可以配置一个id,此外还可以通过order属性指定切面优先级。
声明切入点
切入点使用<aop:pointcut> 元素声明
切入点必须定义在 <aop:aspect> 元素下, 或者直接定义在 <aop:config> 元素下.
--定义在 <aop:aspect> 元素下: 只对当前切面有效
--定义在 <aop:config> 元素下: 对所有切面都有效
基于 XML 的 AOP 配置不允许在切入点表达式中用名称引用其他切入点.
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(* com.atguigu.spring.aop.xml.ArithmeticCalculator.*(..))" id="arithPointcut"/>
声明通知
这里的通知就是指前置、后置、返回、异常、环绕等类型的通知。
在 aop Schema 中, 每种通知类型都对应一个特定的 XML 元素.
前置通知:<aop:before />
后置通知:<aop:after />
返回通知:<aop:after-returning />
异常通知:<aop:after-throwing />
环绕通知:<aop:around />
通知元素需要使用 <pointcut-ref>
来引用切入点, 或用 <pointcut> 直接嵌入切入点表达式. method 属性指定切面类中通知方法的名称.
示例如下:
<!-- 配置AOP -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(* com.atguigu.spring.aop.xml.ArithmeticCalculator.*(..))" id="arithPointcut"/>
<!-- 配置切面及通知 -->
<aop:aspect ref="loggingAspect" order="2">
<aop:before method="beforeMethod" pointcut-ref="arithPointcut"/>
<aop:after method="afterMethod" pointcut-ref="arithPointcut"/>
<!-- 注意这里多了一个returning属性配置,而且其值必须与方法的参数名一致 -->
<aop:after-returning method="returningMethod" pointcut-ref="arithPointcut" returning="result"/>
<!-- 这里必须配置throwing,其值也必须与方法的参数名一致 -->
<aop:after-throwing method="afterThrowing" pointcut-ref="arithPointcut" throwing="e"/>
<!-- 配置环绕通知 -->
<!-- <aop:around method="aroundMethod" pointcut-ref="arithPointcut"/> -->
</aop:aspect>
<aop:aspect ref="validatingAspect" order="1">
<aop:before method="validateArgs" pointcut-ref="arithPointcut"/>
</aop:aspect>
</aop:config>
正常情况下, 基于注解的声明要优先于基于 XML 的声明. 通过 AspectJ 注解, 切面可以与 AspectJ 兼容, 而基于 XML 的配置则是 Spring 专有的. 由于 AspectJ 得到越来越多的 AOP 框架支持, 所以以注解风格编写的切面将会有更多重用的机会.
当使用 XML 声明切面时, 需要在 <beans> 根元素中导入 aop Schema
在 Bean 配置文件中, 所有的 Spring AOP 配置都必须定义在 <aop:config> 元素内部. 对于每个切面而言, 都要创建一个
<aop:aspect> 元素来为具体的切面实现引用后端 Bean 实例.
切面 Bean 必须有一个标示符, 供 <aop:aspect> 元素引用
声明切面
在声明切面之前,需要先为切面类声明一个普通bean。然后在<aop:config>中使用<aop:aspect> 声明为一个切面:
<bean id="arithmeticCalculator"
class="com.atguigu.spring.aop.xml.ArithmeticCalculatorImpl"></bean>
<bean id="loggingAspect"
class="com.atguigu.spring.aop.xml.LoggingAspect"></bean>
<bean id="validatingAspect"
class="com.atguigu.spring.aop.xml.ValidatingAspect"></bean>
<!-- 配置AOP -->
<aop:config>
<!-- 配置切面 -->
<aop:aspect ref="loggingAspect" order="2">
</aop:aspect>
<aop:aspect ref="validatingAspect" order="1">
</aop:aspect>
</aop:config>
在声明切面的时候也可以配置一个id,此外还可以通过order属性指定切面优先级。
声明切入点
切入点使用<aop:pointcut> 元素声明
切入点必须定义在 <aop:aspect> 元素下, 或者直接定义在 <aop:config> 元素下.
--定义在 <aop:aspect> 元素下: 只对当前切面有效
--定义在 <aop:config> 元素下: 对所有切面都有效
基于 XML 的 AOP 配置不允许在切入点表达式中用名称引用其他切入点.
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(* com.atguigu.spring.aop.xml.ArithmeticCalculator.*(..))" id="arithPointcut"/>
声明通知
这里的通知就是指前置、后置、返回、异常、环绕等类型的通知。
在 aop Schema 中, 每种通知类型都对应一个特定的 XML 元素.
前置通知:<aop:before />
后置通知:<aop:after />
返回通知:<aop:after-returning />
异常通知:<aop:after-throwing />
环绕通知:<aop:around />
通知元素需要使用 <pointcut-ref>
来引用切入点, 或用 <pointcut> 直接嵌入切入点表达式. method 属性指定切面类中通知方法的名称.
示例如下:
<!-- 配置AOP -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(* com.atguigu.spring.aop.xml.ArithmeticCalculator.*(..))" id="arithPointcut"/>
<!-- 配置切面及通知 -->
<aop:aspect ref="loggingAspect" order="2">
<aop:before method="beforeMethod" pointcut-ref="arithPointcut"/>
<aop:after method="afterMethod" pointcut-ref="arithPointcut"/>
<!-- 注意这里多了一个returning属性配置,而且其值必须与方法的参数名一致 -->
<aop:after-returning method="returningMethod" pointcut-ref="arithPointcut" returning="result"/>
<!-- 这里必须配置throwing,其值也必须与方法的参数名一致 -->
<aop:after-throwing method="afterThrowing" pointcut-ref="arithPointcut" throwing="e"/>
<!-- 配置环绕通知 -->
<!-- <aop:around method="aroundMethod" pointcut-ref="arithPointcut"/> -->
</aop:aspect>
<aop:aspect ref="validatingAspect" order="1">
<aop:before method="validateArgs" pointcut-ref="arithPointcut"/>
</aop:aspect>
</aop:config>
相关文章推荐
- [学习小结]Spring_通知的那些事和基于配置文件的方式来配置AOP
- Spring_22_基于配置文件的方式来配置 AOP
- Spring4学习笔记-AOP(基于配置文件的方式)
- spring学习笔记(22)----基于配置文件的方式来配置AOP
- Spring中的AOP(七)——基于XML配置文件方式的AOP
- 8.4.6: Spring的AOP---基于XML配置文件的管理方式
- Spring中的AOP(六)——基于XML配置文件方式的AOP
- Spring中基于配置文件的方式来配置AOP
- 20.Spring学习笔记_基于配置文件的方式来配置 AOP(by尚硅谷_佟刚)
- spring之aop编程——基于注解、xml配置文件方式
- Spring笔记5-基于配置文件的方式配置AOP
- Spring中的AOP(六)——基于XML配置文件方式的AOP
- Spring基于配置文件的方式来配置AOP
- Spring4学习笔记-AOP(基于配置文件的方式)
- 8 -- 深入使用Spring -- 4...6 AOP代理:基于注解的XML配置文件的管理方式
- spring Aop 配置文件方式+JoinPoint获取参数
- spring学习笔记(20)——基于配置文件方式配置AOP
- Spring4学习笔记-Spring4整合Struts2(基于配置文件的方式)
- 基于配置文件的方式来配置 AOP