spring官网在线学习文档翻译5

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

spring官⽹在线学习⽂档翻译5
5. Aspect Oriented Programming with Spring(使⽤Spring进⾏⾯向切⾯编程)
5.1. Introduction(⼊门)
Aspect-Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects. (Such concerns are often termed crosscutting concerns in AOP literature.)
⾯向⽅⾯程序设计(AOP)通过提供另⼀种考虑程序结构的⽅法来补充⾯向对象程序设计(OOP)。

OOP中模块性的关键单元是类,⽽AOP中模块性的单元是⽅⾯。

⽅⾯⽀持关注点的模块化,例如跨多个类型和对象的事务管理。

(在AOP⽂献中,这样的关注点通常被称为横切关注点。

)
One of the key components of Spring is the AOP framework. While the Spring IoC container does not depend on AOP, meaning you do not need to use AOP if you don’t want to, AOP complements Spring IoC to provide a very capable middleware solution.
Spring的关键组件之⼀是AOP框架。

虽然Spring IoC容器不依赖于AOP,这意味着如果您不想使⽤AOP,也不需要使⽤AOP,但AOP 对Spring IoC进⾏了补充,提供了⼀个⾮常强⼤的中间件解决⽅案。

Spring 2.0+ AOP
Spring 2.0 introduced a simpler and more powerful way of writing custom aspects using either a or the . Both of these styles offer fully typed advice and use of the AspectJ pointcut language, while still using Spring AOP for weaving.
Spring 2.0引⼊了⼀种更简单、更强⼤的⽅法,可以使⽤基于模式的⽅法或@AspectJ注释风格编写⾃定义⽅⾯。

这两种风格都提供了完全类型的通知和AspectJ切⼊点语⾔的使⽤,同时仍然使⽤Spring AOP进⾏编织。

The Spring 2.0+ schema- and @AspectJ-based AOP support is discussed in this chapter. The lower-level AOP support, as commonly exposed in Spring 1.2 applications, is discussed in .
本章将讨论基于Spring 2.0+ schema和@ aspectj的AOP⽀持。

下⼀章将讨论在Spring 1.2应⽤程序中常见的低层AOP⽀持。

AOP is used in the Spring Framework to…(在Spring框架中使⽤AOP来……)
… provide declarative enterprise services, especially as a replacement for EJB declarative services. The most important such service is .
提供声明性企业服务,特别是作为EJB声明性服务的替代品。

此类服务中最重要的是声明式事务管理。

… allow users to implement custom aspects, complementing their use of OOP with AOP.
允许⽤户实现⾃定义⽅⾯,⽤AOP补充他们对OOP的使⽤。

If you are interested only in generic declarative services or other pre-packaged declarative middleware services such as pooling, you do not need to work directly with Spring AOP, and can skip most of this chapter.
如果您只对通⽤的声明性服务或其他预先打包的声明性中间件服务(如池)感兴趣,那么您不需要直接使⽤Spring AOP,可以跳过本章的⼤部分内容。

5.1.1. AOP concepts()
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 ) or regular classes annotated with the @Aspect annotation (the ).
⽅⾯:跨多个类的关注点的模块化。

事务管理是企业级Java应⽤程序中横切关注点的⼀个很好的例⼦。

在Spring AOP中,⽅⾯是使⽤常规类(基于模式的⽅法)或使⽤@Aspect注释的常规类(@AspectJ风格)实现的。

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.
连接点:程序执⾏期间的⼀个点,例如⽅法执⾏或异常处理期间的⼀个点。

在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.
建议:⽅⾯在特定连接点上采取的操作。

不同类型的建议包括“周围”、“之前”和“之后”的建议。

(下⾯将讨论建议类型。

)许多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.
切⼊点:匹配连接点的谓词。

通知与切⼊点表达式相关联,并在切⼊点匹配的任何连接点上运⾏(例如,具有特定名称的⽅法的执⾏)。

由切⼊点表达式匹配的连接点的概念是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.)介绍:代表类型声明其他⽅法或字段。

Spring AOP允许您向任何被建议的对象引⼊新的接⼝(以及相应的实现)。

例如,您可以使⽤⼀个介绍来让⼀个bean实现⼀个IsModified接⼝,以简化缓存。

(在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.
⽬标对象:由⼀个或多个⽅⾯通知的对象。

也称为被建议的对象。

因为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框架为了实现⽅⾯契约(通知⽅法的执⾏等等)⽽创建的对象。

在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.
编织:将⽅⾯与其他应⽤程序类型或对象链接,以创建建议对象。

这可以在编译时(例如使⽤AspectJ编译器)、加载时或运⾏时完成。

与其他纯Java AOP框架⼀样,Spring 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 throwing advice: Advice to be executed if a method exits by throwing an exception.
抛出通知后:如果⽅法通过抛出异常⽽退出,则执⾏通知。

After (finally) advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).
通知之后(最后):⽆论连接点以何种⽅式退出(正常或异常返回),都要执⾏通知。

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通知可以在⽅法调⽤之前和之后执⾏⾃定义⾏为。

它还负责选择是继续到连接点,还是通过返回⾃⼰的返回值或抛出异常来简化建议的⽅法的执⾏。

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 return advice,⽽不是around advice,尽管around advice可以完成同样的事情。

使⽤最特定的通知类型可以提供更简单的编程模型,出错的可能性更⼩。

例如,您不需要在⽤于around通知的JoinPoint上调⽤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.
在Spring 2.0中,所有通知参数都是静态类型的,因此您可以使⽤适当类型的通知参数(例如⽅法执⾏返回值的类型),⽽不是使⽤对象数组。

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).
由切⼊点匹配的连接点的概念是AOP区别于只提供拦截的旧技术的关键。

切⼊点使通知能够独⽴于⾯向对象的层次结构作为⽬标。

例如,提供声明性事务管理的around通知可以应⽤于⼀组跨越多个对象的⽅法(例如服务层中的所有业务操作)。

5.1.2. Spring AOP capabilities and goals(Spring AOP的功能和⽬标)
Spring AOP is implemented in pure Java. There is no need for a special compilation process. Spring AOP does not need to control the class loader hierarchy, and is thus suitable for use in a Servlet container or application server.
Spring AOP是⽤纯Java实现的。

不需要特殊的编译过程。

Spring AOP不需要控制类装⼊器的层次结构,因此适合在Servlet容器或应⽤服务器中使⽤。

Spring AOP currently supports only method execution join points (advising the execution of methods on Spring beans). Field interception is not implemented, although support for field interception could be added without breaking the core Spring AOP APIs. If you need to advise field access and update join points, consider a language such as AspectJ.
Spring AOP⽬前只⽀持⽅法执⾏连接点(通知⽅法在Spring bean上的执⾏)。

虽然可以在不破坏Spring AOP核⼼api的情况下添加对字段拦截的⽀持,但没有实现字段拦截。

如果需要通知字段访问和更新连接点,请考虑使⽤AspectJ之类的语⾔
Spring AOP’s approach to AOP differs from that of most other AOP frameworks. The aim is not to provide the most complete AOP implementation (although Spring AOP is quite capable); it is rather to provide a close integration between AOP implementation and Spring IoC to help solve common problems in enterprise applications.
:Spring AOP实现AOP的⽅法不同于⼤多数其他AOP框架。

其⽬的不是提供最完整的AOP实现(尽管Spring AOP很有能⼒);⽽是提供AOP实现和Spring IoC之间的紧密集成,以帮助解决企业应⽤程序中的常见问题。

Thus, for example, the Spring Framework’s AOP functionality is normally used in conjunction with the Spring IoC container. Aspects are configured using normal bean definition syntax (although this allows powerful "autoproxying" capabilities): this is a crucial difference from other AOP implementations. There are some things you cannot do easily or efficiently with Spring AOP, such as advise very fine-grained objects (such as domain objects typically): AspectJ is the best choice in such cases. However, our experience is that Spring AOP provides an excellent solution to most problems in enterprise Java applications that are amenable to AOP.
因此,例如,Spring框架的AOP功能通常与Spring IoC容器⼀起使⽤。

⽅⾯是使⽤普通的bean定义语法配置的(尽管这允许强⼤的“⾃动代理”功能):这是与其他AOP实现的⼀个关键区别。

使⽤Spring AOP有⼀些事情是不能轻松或有效地完成的,⽐如通知⾮常细粒度的对象(通常是域对象):在这种情况下,AspectJ是最好的选择。

然⽽,我们的经验是,Spring AOP为企业级Java应⽤程序中的⼤多数问题提供了⼀个优秀的解决⽅案,⽽这些问题都是符合AOP的。

Spring AOP will never strive to compete with AspectJ to provide a comprehensive AOP solution. We believe that both proxy-based frameworks like Spring AOP and full-blown frameworks such as AspectJ are valuable, and that they are complementary, rather than in competition. Spring seamlessly integrates Spring AOP and IoC with AspectJ, to enable all uses of AOP to be catered for within a consistent Spring-based application architecture. This integration does not affect the Spring AOP API or the AOP Alliance API: Spring AOP remains backward-compatible. See for a discussion of the Spring AOP APIs.
Spring AOP永远不会努⼒与AspectJ竞争来提供全⾯的AOP解决⽅案。

我们相信基于代理的框架(如Spring AOP)和成熟的框架(如AspectJ)都是有价值的,⽽且它们是互补的,⽽不是竞争的。

Spring与AspectJ⽆缝地集成了Spring AOP和IoC,使AOP的所有使⽤都能在⼀致的基于Spring的应⽤程序体系结构中得到满⾜。

这种集成不会影响Spring AOP API或AOP Alliance API: Spring AOP保持向后兼容。

有关Spring AOP api的讨论,请参阅下⼀章。

One of the central tenets of the Spring Framework is that of non-invasiveness; this is the idea that you should not be forced to introduce framework-specific classes and interfaces into your business/domain model. However, in some places the Spring Framework does give you the option to introduce Spring Framework-specific dependencies into your codebase: the rationale in giving you such options is because in certain scenarios it might be just plain easier to read or code some specific piece of functionality in such a way. The Spring Framework (almost) always offers you the choice though: you have the freedom to make an informed decision as to which option best suits your particular use case or scenario.One such choice that is relevant to this chapter is that of which AOP framework (and which AOP style) to choose. You have the choice of AspectJ and/or Spring AOP, and you also have the choice of either the @AspectJ annotation-style approach or the Spring XML configuration-style approach. The fact that this chapter chooses to introduce the @AspectJ-style approach first should not be taken as an indication that the Spring team favors the @AspectJ annotation-style approach over the Spring XML configuration-style.See for
a more complete discussion of the whys and wherefores of each style.
Spring框架的核⼼原则之⼀是⾮侵⼊性;这就是不应该强迫您将特定于框架的类和接⼝引⼊您的业务/领域模型的思想。

然⽽,在⼀些地⽅,Spring框架并给你选择春天特定于框架的依赖关系引⼊到你的代码库:原理等给你选择的是,因为在某些情况下它可能只是普通的容易阅读或某些特定功能的代码的⽅式。

不过,Spring框架(⼏乎)总是为您提供选择:您可以⾃由地做出明智的决定,选择哪个选项最适合您的特定⽤例或场景。

与本章相关的⼀个选择是选择哪个AOP框架(以及哪种AOP风格)。

您可以选择AspectJ和/或Spring AOP,也可以选择@AspectJ注释风格的⽅法或Spring XML配置风格的⽅法。

本章选择⾸先介绍@AspectJ风格的⽅法并不意味着Spring团队更喜
欢@AspectJ注释风格的⽅法,⽽不是Spring XML配置风格的⽅法。

有关每种风格的原因和原因的更完整的讨论,请参见选择使⽤哪种AOP声明风格。

5.1.3. AOP Proxies(代理)
Spring AOP defaults to using standard JDK dynamic proxies for AOP proxies. This enables any interface (or set of interfaces) to be proxied.
Spring AOP默认为AOP代理使⽤标准JDK动态代理。

这允许代理任何接⼝(或⼀组接⼝)。

Spring AOP can also use CGLIB proxies. This is necessary to proxy classes rather than interfaces. CGLIB is used by default if a business object does not implement an interface. As it is good practice to program to interfaces rather than classes; business classes normally will implement one or more business interfaces. It is possible to , in those (hopefully rare) cases where you need to advise a method that is not declared on an interface, or where you need to pass a proxied object to a method as a concrete type.
Spring AOP也可以使⽤CGLIB代理。

这对于代理类⽽不是接⼝是必要的。

如果业务对象没有实现接⼝,则默认使⽤CGLIB。

因为这是⼀个很好的做法,编程接⼝⽽不是类;业务类通常会实现⼀个或多个业务接⼝。

在需要通知接⼝上没有声明的⽅法,或者需要将代理对象作为具体类型传递给⽅法的情况下(希望很少),可以强制使⽤CGLIB。

It is important to grasp the fact that Spring AOP is proxy-based. See for a thorough examination of exactly what this implementation detail actually means.
掌握Spring AOP是基于代理的这⼀事实很重要。

请参阅理解AOP代理,了解这个实现细节的真正含义。

5.2. @AspectJ support(⽀持)
@AspectJ refers to a style of declaring aspects as regular Java classes annotated with annotations. The @AspectJ style was introduced by the as part of the AspectJ 5 release. Spring interprets the same annotations as AspectJ 5, using a library supplied by AspectJ for pointcut parsing and matching. The AOP runtime is still pure Spring AOP though, and there is no dependency on the AspectJ compiler or weaver.
@AspectJ引⽤了⼀种将⽅⾯声明为带有注释的常规Java类的样式。

@AspectJ样式是由AspectJ项⽬作为AspectJ 5发⾏版的⼀部分引⼊的。

Spring使⽤AspectJ提供的⽤于切⼊点解析和匹配的库来解释与AspectJ 5相同的注释。

AOP运⾏时仍然是纯粹的Spring AOP,⽽且不依赖于AspectJ编译器或weaver。

Using the AspectJ compiler and weaver enables use of the full AspectJ language, and is discussed in .
使⽤AspectJ编译器和weaver使使⽤完整的AspectJ语⾔成为可能,将在与Spring应⽤程序⼀起使⽤AspectJ中进⾏讨论。

5.2.1. Enabling @AspectJ Support(⽀持@ aspectj的⽀持)
To use @AspectJ aspects in a Spring configuration you need to enable Spring support for configuring Spring AOP based on @AspectJ aspects, and autoproxying beans based on whether or not they are advised by those aspects. By autoproxying we mean that if Spring determines that a bean is advised by one or more aspects, it will automatically generate a proxy for that bean to intercept method invocations and ensure that advice is executed as needed.
要在Spring配置中使⽤@AspectJ⽅⾯,您需要启⽤Spring⽀持来配置基于@AspectJ⽅⾯的Spring AOP,并根据这些⽅⾯是否建议⾃动代理bean。

通过⾃动代理,我们的意思是,如果Spring确定⼀个bean被⼀个或多个⽅⾯通知,它将⾃动为该bean⽣成⼀个代理来拦截⽅法调⽤,并确保在需要时执⾏通知。

The @AspectJ support can be enabled with XML or Java style configuration. In either case you will also need to ensure that AspectJ’s aspectjweaver.jar library is on the classpath of your application (version 1.8 or later). This library is available in the 'lib' directory of an AspectJ distribution or via the Maven Central repository.
可以通过XML或Java风格配置启⽤@AspectJ⽀持。

在这两种情况下,您还需要确保AspectJ的aspectjwever .jar库位于应⽤程序的类路径中(版本1.8或更⾼)。

这个库可以在AspectJ发⾏版的“lib”⽬录中获得,也可以通过Maven中央存储库获得。

Enabling @AspectJ Support with Java configuration(使⽤Java配置启⽤@AspectJ⽀持)
To enable @AspectJ support with Java @Configuration add the @EnableAspectJAutoProxy annotation:
要使⽤Java @Configuration启⽤@AspectJ⽀持,添加@EnableAspectJAutoProxy注释:
@Configuration
@EnableAspectJAutoProxy
public class AppConfig {
}
Enabling @AspectJ Support with XML configuration(使⽤XML配置启⽤@AspectJ⽀持)
To enable @AspectJ support with XML based configuration use the aop:aspectj-autoproxy element:
使⽤aop:aspectj-autoproxy元素来启⽤基于XML的配置@AspectJ⽀持:
<aop:aspectj-autoproxy/>
This assumes that you are using schema support as described in . See for how to import the tags in the aop namespace.
这假设您正在使⽤XML基于模式的配置中描述的模式⽀持。

请参阅AOP模式,了解如何在AOP名称空间中导⼊标记。

5.2.2. Declaring an aspect(声明⼀个⽅⾯)
With the @AspectJ support enabled, any bean defined in your application context with a class that is an @AspectJ aspect (has the @Aspect annotation) will be automatically detected by Spring and used to configure Spring AOP. The following example shows the minimal definition required for a not-very-useful aspect:
启⽤了@AspectJ⽀持后,在您的应⽤程序上下⽂中定义的带有@AspectJ⽅⾯(具有@Aspect注释)类的任何bean都将被Spring⾃动检测并⽤于配置Spring AOP。

下⾯的例⼦显⽰了⼀个不太有⽤的⽅⾯所需的最⼩定义:
A regular bean definition in the application context, pointing to a bean class that has the @Aspect annotation:
应⽤程序上下⽂中的⼀个常规bean定义,指向⼀个带有@Aspect注释的bean类:
<bean id="myAspect" class="org.xyz.NotVeryUsefulAspect">
<!-- configure properties of aspect here as normal -->
</bean>
And the NotVeryUsefulAspect class definition, annotated with ng.annotation.Aspect annotation;
和NotVeryUsefulAspect类定义,⽤ng.annotation注释。

注释⽅⾯;
package org.xyz;
import ng.annotation.Aspect;
@Aspect
public class NotVeryUsefulAspect {
}
Aspects (classes annotated with @Aspect) may have methods and fields just like any other class. They may also contain pointcut, advice, and introduction (inter-type) declarations.
⽅⾯(⽤@Aspect注释的类)可能像其他类⼀样有⽅法和字段。

它们还可能包含切⼊点、通知和引⼊(类型间)声明。

Autodetecting aspects through component scanningYou may register aspect classes as regular beans in your Spring XML configuration, or autodetect them through classpath scanning - just like any other Spring-managed bean. However, note that the @Aspect annotation is not sufficient for autodetection in the classpath: For that purpose, you need to add a separate
@Component annotation (or alternatively a custom stereotype annotation that qualifies, as per the rules of Spring’s component
scanner).
Advising aspects with other aspects?In Spring AOP, it is not possible to have aspects themselves be the target of advice from other aspects. The @Aspect annotation on a class marks it as an aspect, and hence excludes it from auto-proxying.
通过组件扫描⾃动检测各个⽅⾯
您可以在Spring XML配置中将⽅⾯类注册为常规bean,或者通过类路径扫描⾃动检测它们—就像任何其他Spring管理的bean⼀样。

然⽽,请注意@Aspect注释对于类路径中的⾃动检测是不够的:为了这个⽬的,您需要添加⼀个单独的@Component注释(或者根据Spring的组件扫描器的规则,⼀个定制的构造型注释)。

对其他⽅⾯提出建议?
在Spring AOP中,不可能让⽅⾯本⾝成为来⾃其他⽅⾯的通知的⽬标。

类上的@Aspect注释将其标记为⼀个⽅⾯,因此将其从⾃动代理中排除。

5.2.3. Declaring a pointcut(声明⼀个切⼊点)
Recall that pointcuts determine join points of interest, and thus enable us to control when advice executes. Spring AOP only supports method execution join points for Spring beans, so you can think of a pointcut as matching the execution of methods on Spring beans. A pointcut declaration has two parts: a signature comprising a name and any parameters, and a pointcut expression that determines exactly which method executions we are interested in. In the @AspectJ annotation-style of AOP, a pointcut signature is provided by a regular method definition, and the pointcut expression is indicated using the @Pointcut annotation (the method serving as the pointcut signature must have a void return type).
回想⼀下,切⼊点决定了感兴趣的连接点,从⽽使我们能够控制通知何时执⾏。

Spring AOP只⽀持Spring bean的⽅法执⾏连接点,所以您可以将切⼊点看作是匹配Spring bean上⽅法的执⾏。

切⼊点声明有两个部分:由名称和任何参数组成的签名,以及准确确定我们感兴趣的⽅法执⾏的切⼊点表达式。

在@AspectJ注释风格的AOP中,⼀个切⼊点签名是由⼀个常规⽅法定义提供的,⽽切⼊点表达式是使⽤@Pointcut注释表⽰的(作为切⼊点签名的⽅法必须有⼀个void返回类型)。

An example will help make this distinction between a pointcut signature and a pointcut expression clear. The following example defines a pointcut named 'anyOldTransfer' that will match the execution of any method named 'transfer':
⼀个⽰例将有助于清晰地区分切⼊点签名和切⼊点表达式。

下⾯的例⼦定义了⼀个名为“anyOldTransfer”的切⼊点,它将匹配任何名为“transfer”的⽅法的执⾏:
@Pointcut("execution(* transfer(..))")// the pointcut expression
private void anyOldTransfer() {}// the pointcut signature
The pointcut expression that forms the value of the @Pointcut annotation is a regular AspectJ 5 pointcut expression. For a full discussion of AspectJ’s pointcut language, see the (and for extensions, the ) or one of the books on AspectJ such as "Eclipse AspectJ" by Colyer et. al. or "AspectJ in Action" by Ramnivas Laddad.
构成@Pointcut注释值的切⼊点表达式是⼀个常规的AspectJ 5切⼊点表达式。

关于AspectJ的切⼊点语⾔的完整讨论,请参阅AspectJ 编程指南(关于扩展,请参阅AspectJ 5开发⼈员笔记)或⼀本关于AspectJ的书,⽐如Colyer等⼈写的“Eclipse AspectJ”或Ramnivas Laddad写的“AspectJ in Action”。

Supported Pointcut Designators(⽀持切⼊点指⽰器)
Spring AOP supports the following AspectJ pointcut designators (PCD) for use in pointcut expressions:
Spring AOP⽀持在切⼊点表达式中使⽤以下AspectJ切⼊点指⽰器(PCD):
Other pointcut types(其他类型的切⼊点)
The full AspectJ pointcut language supports additional pointcut designators that are not supported in Spring. These are: call, get, set, preinitialization, staticinitialization, initialization, handler, adviceexecution, withincode, cflow, cflowbelow, if, @this, and @withincode. Use of these pointcut designators in pointcut expressions interpreted by Spring AOP will result in an IllegalArgumentException being thrown.
完整的AspectJ切⼊点语⾔⽀持Spring中不⽀持的附加切⼊点指⽰器。

这些是:调⽤,获取,设置,预初始化,静态初始化,初始化,处理器,adviceexecution,内部代码,cflow, cflowbelow, if, @this,和@内thincode。

在由Spring AOP解释的切⼊点表达式中使⽤这些切⼊点指⽰器将导致抛出⼀个IllegalArgumentException。

The set of pointcut designators supported by Spring AOP may be extended in future releases to support more of the AspectJ pointcut designators.
Spring AOP⽀持的切⼊点指⽰器集可能在未来的版本中得到扩展,以⽀持更多的AspectJ切⼊点指⽰器。

execution - for matching method execution join points, this is the primary pointcut designator you will use when working with Spring AOP
执⾏——为了匹配⽅法执⾏连接点,这是在使⽤Spring AOP时将使⽤的主要切⼊点指⽰器
within - limits matching to join points within certain types (simply the execution of a method declared within a matching type when using Spring AOP)
限定对某些类型内的连接点的匹配(在使⽤Spring AOP时,简单地执⾏在匹配类型内声明的⽅法)
this - limits matching to join points (the execution of methods when using Spring AOP) where the bean reference (Spring AOP proxy) is an instance of the given type
这—将匹配限制为连接点(使⽤Spring AOP时⽅法的执⾏),其中bean引⽤(Spring AOP代理)是给定类型的实例
target - limits matching to join points (the execution of methods when using Spring AOP) where the target object (application object being proxied) is an instance of the given type
⽬标——限制对连接点(使⽤Spring AOP时⽅法的执⾏)的匹配,其中⽬标对象(被代理的应⽤程序对象)是给定类型的实例
args - limits matching to join points (the execution of methods when using Spring AOP) where the arguments are instances of the given types
args—限制对连接点(使⽤Spring AOP时⽅法的执⾏)的匹配,其中参数是给定类型的实例
@target - limits matching to join points (the execution of methods when using Spring AOP) where the class of the executing object has an annotation of the given type
@target—限制对连接点(使⽤Spring AOP时⽅法的执⾏)的匹配,其中执⾏对象的类具有给定类型的注释@args - limits matching to join points (the execution of methods when using Spring AOP) where the runtime type of the actual
arguments passed have annotations of the given type(s)
@args—限制对连接点的匹配(使⽤Spring AOP时⽅法的执⾏),其中传递的实际参数的运⾏时类型具有给定类型的注释@within - limits matching to join points within types that have the given annotation (the execution of methods declared in types with
the given annotation when using Spring AOP)
@within—与具有给定注释的类型中的连接点匹配的限制(使⽤Spring AOP时使⽤给定注释执⾏类型中声明的⽅法) @annotation - limits matching to join points where the subject of the join point (method being executed in Spring AOP) has the given annotation
@annotation——限制对连接点主题(在Spring AOP中执⾏的⽅法)具有给定注释的连接点的匹配
Because Spring AOP limits matching to only method execution join points, the discussion of the pointcut designators above gives a narrower definition than you will find in the AspectJ programming guide. In addition, AspectJ itself has type-based semantics and at an execution join point both this and target refer to the same object - the object executing the method. Spring AOP is a proxy-based system and differentiates between the proxy object itself (bound to this) and the target object behind the proxy (bound to target).
因为Spring AOP将匹配限制为只匹配⽅法执⾏连接点,所以上⾯关于切⼊点指⽰器的讨论给出了⼀个⽐AspectJ编程指南中更窄的定义。

此外,AspectJ本⾝具有基于类型的语义,并且在执⾏连接点上,this和target都引⽤相同的对象—执⾏⽅法的对象。

Spring AOP 是⼀个基于代理的系统,区分了代理对象本⾝(绑定到它)和代理背后的⽬标对象(绑定到target)。

Due to the proxy-based nature of Spring’s AOP framework, calls within the target object are by definition not intercepted. For JDK proxies, only public interface method calls on the proxy can be intercepted. With CGLIB, public and protected method calls on the proxy will be intercepted, and even package-visible methods if necessary. However, common interactions through proxies should always be designed through public signatures.Note that pointcut definitions are generally matched against any intercepted method. If a pointcut is strictly meant to be public-only, even in a CGLIB proxy scenario with potential non-public interactions through proxies, it needs to be defined accordingly.If your interception needs include method calls or even constructors within the target class, consider the use of Spring-driven instead of Spring’s proxy-based AOP framework. This constitutes a different mode of AOP usage with different characteristics, so be sure to make yourself familiar with weaving first
before making a decision.
由于Spring s AOP框架基于代理的本质,⽬标对象内的调⽤根据定义是不被拦截的。

对于JDK代理,只能拦截代理上的公共接⼝⽅法调⽤。

使⽤CGLIB,代理上的公共和受保护的⽅法调⽤将被拦截,如果需要,甚⾄可以拦截包可见的⽅法。

但是,通过代理的常见交互应该始终通过公共签名来设计。

注意,切⼊点定义通常与任何截获的⽅法相匹配。

如果严格意义上说切⼊点是只公开的,即使是在CGLIB代理场景中,通过代理进⾏潜在的⾮公开交互,也需要相应地定义它。

如果您的拦截需要包括⽬标类中的⽅法调⽤甚⾄构造函数,请考虑使⽤Spring驱动的本地AspectJ编织,⽽不是基于Spring代理的AOP框架。

这构成了具有不同特征的不同AOP使⽤模式,因此在做出决定之前,⼀定要先熟悉编织。

Spring AOP also supports an additional PCD named bean. This PCD allows you to limit the matching of join points to a particular named Spring bean, or to a set of named Spring beans (when using wildcards). The bean PCD has the following form:
Spring AOP还⽀持另外⼀个名为bean的PCD。

这个PCD允许您将连接点的匹配限制为特定的命名Spring bean或⼀组命名Spring bean(当使⽤通配符时)。

bean PCD有以下形式:
bean(idOrNameOfBean)
The idOrNameOfBean token can be the name of any Spring bean: limited wildcard support using the * character is provided, so if you establish some naming conventions for your Spring beans you can quite easily write a bean PCD expression to pick them out. As is the case with other pointcut designators, the bean PCD can be &&'ed, ||'ed, and ! (negated) too.
id装饰eofbean标记可以是任何Spring bean的名称:提供了使⽤*字符的有限通配符⽀持,因此如果您为Spring bean建⽴了⼀些命名约定,您可以很容易地编写⼀个bean PCD表达式来将它们挑选出来。

与其他切⼊点指⽰器的情况⼀样,bean PCD可以被&&'ed、||'ed 和!也(否定)。

Please note that the bean PCD is only supported in Spring AOP - and not in native AspectJ weaving. It is a Spring-specific extension to the standard PCDs that AspectJ defines and therefore not available for aspects declared in the @Aspect model.The bean PCD operates at the instance level (building on the Spring bean name concept) rather than at the type level only (which is what weaving-based AOP is limited to). Instance-based pointcut designators are a special capability of Spring’s proxy-based
AOP framework and its close integration with the Spring bean factory, where it is natural and straightforward to identify specific
beans by name.
请注意,bean PCD仅在Spring AOP中得到⽀持——⽽在本地AspectJ编织中没有得到⽀持。

它是AspectJ定义的标准pcd的⼀个特定于spring的扩展,因此对于在@Aspect模型中声明的⽅⾯不可⽤。

bean PCD在实例级操作(构建在Spring bean名称概念上),⽽不是仅在类型级操作(这是基于编织的AOP的限制)。

基于实例的切⼊点指⽰器是Spring s基于代理的AOP框架及其与Spring bean⼯⼚的紧密集成的⼀种特殊功能,在Spring bean⼯⼚中,通过名称识别特定的bean是⾃然⽽直接的。

Combining pointcut expressions(结合切⼊点表达式)
Pointcut expressions can be combined using '&&', '||' and '!'. It is also possible to refer to pointcut expressions by name. The following example shows three pointcut expressions: anyPublicOperation (which matches if a method execution join point represents the execution of any public method); inTrading (which matches if a method execution is in the trading module), and tradingOperation (which matches if a method execution represents any public method in the trading module).。

相关文档
最新文档