13.1.1. AOP concepts
Let us begin by defining some central AOP concepts. These terms are not Spring.NET-specific. Unfortunately,
AOP terminology is not particularly intuitive. However, it would be even more confusing if Spring.NET used
its own terminology.
• Aspect: A modularization of a concern for which the implementation might otherwise cut across multiple
objects. Transaction management is a good example of a crosscutting concern in enterprise applications.
Aspects are implemented using Spring.NET as Advisors or interceptors.
• Joinpoint: Point during the execution of a program, such as a method invocation or a particular exception being
thrown.
• Advice: Action taken by the AOP framework at a particular joinpoint. Different types of advice include
"around," "before" and "throws" advice. Advice types are discussed below. Many AOP frameworks, including
Spring.NET, model an advice as an interceptor, maintaining a chain of interceptors "around" the joinpoint.
• Pointcut: A set of joinpoints specifying when an advice should fire. An AOP framework must allow developers
to specify pointcuts: for example, using regular expressions.
• Introduction: Adding methods or fields to an advised class. Spring.NET allows you to introduce new interfaces
to any advised object. For example, you could use an introduction to make any object implement an IAuditable
interface, to simplify the tracking of changes to an object's state.
Aspect Oriented Programming with Spring.NET
Spring Framework (Version 1.3.0) 130
• Target object: Object containing the joinpoint. Also referred to as advised or proxied object.
• AOP proxy: Object created by the AOP framework, including advice. In Spring.NET, an AOP proxy is a
dynamic proxy that uses IL code generated at runtime.
• Weaving: Assembling aspects to create an advised object. This can be done at compile time (using the Gripper-
Loom.NET compiler, for example), or at runtime. Spring.NET performs weaving at runtime.
Different advice types include:
• Around advice: Advice that surrounds a joinpoint such as a method invocation. This is the most powerful kind
of advice. Around advice will perform custom behaviour before and after the method invocation. They are
responsible for choosing whether to proceed to the joinpoint or to shortcut executing by returning their own
return value or throwing an exception.
• Before advice: Advice that executes before a joinpoint, but which does not have the ability to prevent execution
flow proceeding to the joinpoint (unless it throws an exception).
• Throws advice: Advice to be executed if a method throws an exception. Spring.NET provides strongly typed
throws advice, so you can write code that catches the exception (and subclasses) you're interested in, without
needing to cast from Exception.
• After returning advice: Advice to be executed after a joinpoint completes normally: for example, if a method
returns without throwing an exception.
Spring.NET provides a full range of advice types. We recommend that you use the least powerful advice type
that can implement the required behaviour. For example, if you need only to update a cache with the return value
of a method, you are better off implementing an after returning advice than an around advice, although an around
advice can accomplish the same thing. Using the most specific advice type provides a simpler programming
model with less potential for errors. For example, you don't need to invoke the proceed() method on the
IMethodInvocation used for around advice, and hence can't fail to invoke it.
The pointcut concept is the key to AOP, distinguishing AOP from older technologies offering interception.
Pointcuts enable advice to be targeted independently of the OO hierarchy. For example, an around advice
providing declarative transaction management can be applied to a set of methods spanning multiple objects. Thus
pointcuts provide the structural element of AOP.
首先我们来了解AOP中的一些基本概念。这些概念都是AOP的通用术语,并非Spring.NET所特有。很遗憾AOP的术语不是特别的直观。但如果让Spring.NET来定义自己的专用名词,可能会更加教人糊涂。
-
方面(Aspect):对横向分布在多个对象中的关注点所做的模块化。在企业应用中,事务管理就是一个典型的横切关注点。Spring.NET将方面实现为Advisor或拦截器(interceptor)。(按:Advisor是通知和切入点的组合,拦截器实际就是指通知,注意在本文档中,一般会把环绕通知称为拦截器,而将其它类型的通知称为通知,这是因为环绕通知实现的是AopAlliance.Intercept.IMethodInterceptor接口,而其它通知类型实现的都是Spring.Aop命名空间下的通知接口。)
-
连接点(Joinpoint):程序执行过程中的一个点,例如对某个方法的调用或者某个特定异常的抛出都可以称为连接点。
-
通知(Advice):AOP框架在某个连接点所采取的行为。通知有多种类型,包括“环绕”通知,“前置”通知和“异常”通知等,后文将对通知类型进行讨论。包括Spring.NET在内的很多AOP框架都把通知建模为拦截器(interceptor),并且会维护一个"包围"在连接点周围的拦截器链。
-
切入点(Pointcut):指通知的应用条件,用于确定某个通知要被应用到哪些连接点上。AOP框架应允许让开发人员指定切入点,例如,可以使用正则表达式来指定一个切入点。
-
引入(Introduction):向目标对象添加方法或字段的行为。Spring.NET允许为任何目标对象引入新的接口。例如,可以利用引入让任何对象在运行期实现IAuditable接口,以简化对象状态变化的跟踪过程。(按:也称为mixin,混入)
-
目标对象(Target
object):指包含连接点的对象。也称为被通知或被代理对象。(按:“被通知对象”实际是“被应用了通知的对象”,在译文中,将advised
object或proxied object统称为目标对象,这样更为统一)
-
AOP代理(AOP
proxy):由AOP框架在将通知应用于目标对象后创建的对象。在Spring.NET中,AOP代理是使用IL代码在运行时创建的动态代理。
-
织入(Weaving):将方面进行组装,以创建一个目标对象。织入可以在编译期完成(例如使用Gripper_Loom.NET编译器),也可以在运行时完成。Spring.NET在运行时执行织入。