spring aop
<!--
AOP:面向切面编程
通知(Advice):通知就是切面要完成的工作,通知定义了切面是什么以及何时使用。除了描述切面要完成的工作,通知还解决了何时执行这个工作的问题。
Spring切面可以应用5种类型的通知
1.Before:在方法被调用前调用通知
2.After:在方法完成之后调用通知,无论方法执行是否成功。
3.After-returning:在方法成功执行之后调用通知
4.After-throwing:在方法抛出异常之后调用通知
5.Around:通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为。
连接点(Joinpoint):时机被称为连接点。连接点实在应用执行过程中能够插入切面的一个点。这个点可以是调用方法时、抛出异常时、甚至修改一个字段时。
切面代码可以利用这些点插入到应用的正常流程之中,并添加新的行为。
切点(Poincut):一个切面并不需要通知应用的所有连接点。切点有助于缩小切面所通知连接点的范围。切点的定义会匹配通知所要织入的一个或多个连接点。
切面(Aspect):切面是通知和切点的结合。通知和切点共同定义了关于切面的全部内容。即它是什么,在何时和何处完成其功能。
引入(Introduction):引入允许我们向现有的类添加新方法或属性。
织入(Weaving):织入是将切面应用到目标对象来创建新的代理对象的过程。切面在指定的连接点被织入到目标对象中。在目标对象的生命周日里有多个点可以被织入。
Spring提供了4中各具特色的AOP支持:
1.基于代理的经典AOP
2.@AspectJ注解驱动的切面
3.纯POJO切面
4.注入式AspectJ切面
Spring基于动态代理,所以Spring只支持方法连接点。
execution()指示器选择Instrument的play()方法:
execution( * com.bean.Instrument.play(..)) && within(com.nec.dio.*)
| | | | | | | |
方法执行时触发 返回值 方法所属包 类 方法 任意参数 与操作符 仅匹配该包下
bean()指示器:通过id选择bean
Spring的AOP配置元素
----------------------------------------------------------------------
AOP配置元素 |描述
----------------------------------------------------------------------
<aop:advisor> |定义AOP通知器
----------------------------------------------------------------------
<aop:after> |定义aop后置通知(不管被通知的方法是否执行成功)
----------------------------------------------------------------------
<aop:after-returning> |定义AOP after-returning通知
----------------------------------------------------------------------
<aop:after-throwing> |定义after-throwing通知
----------------------------------------------------------------------
<aop:around> |定义AOP环绕通知
----------------------------------------------------------------------
<aop:aspect> |定义切面
----------------------------------------------------------------------
<aop:aspectj-autoproxy> |启用@AspectJ注解驱动的切面
----------------------------------------------------------------------
<aop:before> |定义AOP前置通知
----------------------------------------------------------------------
<aop:config> |顶层的AOP配置元素,大多数的<aop:*>元素必须包含在<sop:config>元素内
----------------------------------------------------------------------
<aop:declare-parents> |违背通知的对象引入额外的接口,并透明的实现
----------------------------------------------------------------------
<aop:pointcut> |定义切点
----------------------------------------------------------------------
-->
<bean id="audience" class="com.bean。Audience"/>
<!--使用Spring AOP配置元素声明一个audience切面 -->
<aop:config>
<aop:aspect ref="audience">
<aop:before method="takeSeats" pointcut="execution(* com.bean.perform(..))"/>
<aop:before method="turnOffCellPhones" pointcut="execution(* com.bean.perform(..))"/>
<aop:after-returning method="applaud" pointcut="execution(* com.bean.perform(..))"/>
<aop:after-throwing method="demandRefund" pointcut="execution(* com.bean.perform(..))"/>
</aop:aspect>
</aop:config>
<!-- 上面定义中,似乎切点部分重复,我们可以单独定义切点再引用 -->
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut expression="execution(* com.bean.perform(..))" id="performance"/>
<aop:before method="takeSeats" pointcut-ref="performance"/>
<aop:before method="turnOffCellPhones" pointcut-ref="performance"/>
<aop:after-returning method="applaud" pointcut-ref="performance"/>
<aop:after-throwing method="demandRefund" pointcut-ref="performance"/>
</aop:aspect>
</aop:config>