AOP 概念

AOP concepts

AOP 概念

Let us begin by defining some central AOP concepts and terminology. These terms are not Spring-specific... unfortunately, AOP terminology is not particularly intuitive; however, it would be even more confusing if Spring used its own terminology.

让我们从定义一些AOP的核心概念和定义开始。这些定义不是Spring特有的,不幸的是,AOP的定义不是非常的直观;但是,如果Spring用它自己的定义可能会更加难以理解。

 

Aspect: a modularization of a concern that cuts across multiple classes. Transaction management is a good example of a crosscutting concern in enterprise Java applications. In Spring AOP, aspects are implemented using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (the @AspectJ style).

方面(Aspect):指穿过多个类问题的模块化概念。在java企业应用中,事务管理是切面问题的一个很好的例子。在Spring AOP中,Aspect使用一个常规的类实现的(the schema-based approach),或者这些常规的类被注解成@Aspect注解(the @AspectJ style)。

 

Join point: a point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution.

连接点(Join point):在程序执行过程中的点,例如方法执行或者异常处理。在Spring AOP中,一个连接点总是指一个方法的执行。

 

Advice: action taken by an aspect at a particular join point. Different types of advice include "around," "before" and "after" advice. (Advice types are discussed below.) Many AOP frameworks, including Spring, model an advice as an interceptor, maintaining a chain of interceptors around the join point.

通知(Advice):某特定连接点上一个方面所采取的动作。通知包括"around," "before" and "after"等三种类型的通知 (后面会详细介绍这三种类型)。很多的AOP框架(包括Spring),将通知模块化为拦截器,并且在连接点周围保持这个拦截器的链。

 

Pointcut: a predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.

切入点(Pointcut:一个用于匹配连接点的语言表达式。通知与一个切入点表达式结合并且运行在匹配切入点表达式的连接点上运行(例如:具有某一特定名称的方法的执行)。作为匹配切入点表达式的链接点概念是AOP的核心,而且Spring默认使用AspectJ的切入点表达式。

 

Introduction: declaring additional methods or fields on behalf of a type. Spring AOP allows you to introduce new interfaces (and a corresponding implementation) to any advised object. For example, you could use an introduction to make a bean implement an IsModified interface, to simplify caching. (An introduction is known as an inter-type declaration in the AspectJ community.)

引入(Introduction):声明一个类型的其他方法或字段。Spring AOP允许你为任何通知对象引入新的接口(或相应的实现)。例如,你可以用一个引入是一个bean实现一个可以修改的接口,来作为一个简单的缓存(一个引入可以被看做是AspectJ体系中的一个内部类型的声明)。

 

Target object: object being advised by one or more aspects. Also referred to as the advised object. Since Spring AOP is implemented using runtime proxies, this object will always be a proxied object.

目标对象(Target object):被一个或多个方面通知的对象。因为Spring AOP使用运行时代理实现的,所以这个对象通常是被代理对象。

 

AOP proxy: an object created by the AOP framework in order to implement the aspect contracts (advise method executions and so on). In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.

AOP代理(AOP proxy):AOP框架为了实现方面的协议(通知方法执行等等)而创建的一个对象。在Spring 框架中,AOP代理是一个JDK动态代理或者一个CGLIB代理。

 

 

Weaving: linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.

编织(Weaving):将方面和其他的应用类型或对象连接起来并且创建一个被通知的对象。这个操作可以在编译时,加载时或者运行时执行。Spring AOP,像其他的的纯java AOP框架一样在运行时执行编织。

 

Types of advice:

通知的类型:

Before advice: Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).

Before advice:这是一个在连接点前执行的通知,但是它不能够阻止执行流程进入连接点,除非它跑出了一个异常

 

After returning advice: Advice to be executed after a join point completes normally: for example, if a method returns without throwing an exception.

After returning advice:只是一个通常在连接点后执行的通知,例如,如果一个方法返回了结果并且没有抛出异常的情况。

 

After throwing advice: Advice to be executed if a method exits by throwing an exception.

After throwing advice:如果一个方法通过抛出一个异常,这个通知将被执行。

 

After (finally) advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).

After (finally) advice:不考虑连接点是否存在,无论是正常情况还是异常情况,都会被执行的通知。

 

 

Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.

Around advice:这是一个通知,一个包围了一个连接点(例如一个方法调用)。这是最强大的通知类型。Around advice能够在方法调用之前和之后自定义的执行行为。他还能选择是执连接点还是跳过通知方法执行后者抛出异常。

 

Around advice is the most general kind of advice. Since Spring AOP, like AspectJ, provides a full range of advice types, we recommend that you use the least powerful advice type that can implement the required behavior. 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 do not need to invoke the proceed() method on the JoinPoint used for around advice, and hence cannot fail to invoke it.

Around advice是通知的最通用的类型。因为Spring AOP,像AspectJ,提供了一套完善的通知类型,我们推荐你使用功能最小但是又能满足需求的通知类型。例如,如果你只想在一个方法返回值是更新缓存,你最好实现一个after returning 的通知而不是一个around advice,即使一个around advice也能够完成相同的事。使用最个性的通知类型能够提供一种犯错误机会更少的的简单的编程模型。例如,你不需要为around advice调用连接点的proceed() 方法,并且从而导致调用失败。

 

In Spring 2.0, all advice parameters are statically typed, so that you work with advice parameters of the appropriate type (the type of the return value from a method execution for example) rather than Object arrays.

The concept of join points, matched by pointcuts, is the key to AOP which distinguishes it from older technologies offering only interception. Pointcuts enable advice to be targeted independently of the Object-Oriented hierarchy. For example, an around advice providing declarative transaction management can be applied to a set of methods spanning multiple objects (such as all business operations in the service layer).

在Spring2.0中,所有的通知参数都是静态类型,因此你使用被推荐类型的通知参数而不是对象数组的方式。

被切入点匹配了的连接点的概念是AOP的关键,它区别了旧的拦截器技术。切入点是通知具有了定位在面向对象的继承依赖。例如,around advice使声明式事务管理能够以用于一组多个对象的一组方法,例如服务层的全部业务操作。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值