Spring中的面向切面编程外文翻译资料

 2022-03-30 09:03

5. Aspect Oriented Programming with 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.)

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 donrsquo;t want to, AOP complements Spring IoC to provide a very capable middleware solution.

Spring 2.0 AOP

Spring 2.0 introduced a simpler and more powerful way of writing custom aspects using either a schema-based approachor the @AspectJ annotation style. Both of these styles offer fully typed advice and use of the AspectJ pointcut language, while still using Spring AOP for weaving.

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 the following chapter.

AOP is used in the Spring Framework tohellip;​

  • hellip;​ provide declarative enterprise services, especially as a replacement for EJB declarative services. The most important such service is declarative transaction management.
  • hellip;​ allow users to implement custom aspects, complementing their use of OOP with AOP.

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.

5.1.1. AOP concepts

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

  • 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 (theschema-based approach) or regular classes annotated with the @Aspect annotation (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.
  • 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.
  • 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.
  • 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.)
  • 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.
  • 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.
  • 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.

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).
  • 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 m

    全文共22227字,剩余内容已隐藏,支付完成后下载完整资料


    5. Spring中的面向切面编程

    5.1 简介

    面向切面编程(AOP)通过提供另一种程序结构的思路补充了面向对象编程(OOP)。在OOP中关键的模块单元是类(class),而在AOP中模块单元是切面(aspect)。切面使我们的关注点模块化,如横切多种类型和对象的事务管理(这种关注点在AOP中经常被称为横切关注点)。

    Spring中的关键组件之一就是AOP。Spring IoC容器并不依赖于AOP,这意味着如果不想用AOP你就可以不用,AOP提供了一种非常强大的中间件解决方案来补充Spring IoC。

    Spring 2.0 AOP

    Spring 2.0 提供了更简单、更强大的基于schema(XML配置开发)的方式和AspectJ注解方式来进行自定义的切面编程。这两种方式都提供过了所有的通知(advice)类型以及AspectJ的切点(pointcut)表达式语言。

    本章讲解的就是Spring 2.0 提供的基于schema的和基于AspectJ的AOP编程。较老版本的AOP,就是在Spring 1.2中使用的AOP,将在后续章节讲解。

    Spring中的AOP为应用程序提供了如下功能:

    • hellip;提供了声明式企业服务,特别是提供了EJB声明式服务的替代方案。最重要的服务就是声明式事务管理。
    • hellip;允许用户去实现自定义的切面,使用AOP去补充他们的OOP编程。
    • 如果你只对通用的声明式服务或者其他预包装的声明式中间件服务如池(pooling)感兴趣,你就不需要直接使用Spring AOP,可以跳过大部分的章节。

    5.1.1 AOP的概念

    我们从一些AOP核心概念和术语开始学习。这些术语并不是AOP特有的hellip;而且AOP的术语并不是很直观,然而如果使用Spring它自己的术语将会变得更加令人迷惑。

    • 切面(Aspect):横切很多类的模块化关注点。在Java企业级开发中事务管理就是一个很好的例子。在Spring AOP中,切面就是一些普通的类(基于schema的方式)或者是使用了@AspectJ注解的普通的类(AspectJ注解方式)。
    • 连接点(Joinpoint):程序执行中的一个点,如方法执行或者异常处理。在Spring AOP中,一个连接点通常表现为方法的执行。
    • 通知(Advice):在切面的某个特定的连接点(Joinpoint)上执行的动作。通知有各种类型,其中包括“around”、 “before”和“after”等通知。通知的类型将在后面部分进行讨论。许多AOP框架,包括Spring,都是以拦截器做通知模型, 并维护一个以连接点为中心的拦截器链。
    • 切入点(Pointcut):匹配连接点(Joinpoint)的断言。通知和一个切入点表达式关联,并在满足这个切入点的连接点上运行(例 如,当执行某个特定名称的方法时)。切入点表达式如何和连接点匹配是AOP的核心:Spring缺省使用AspectJ切入点语法。
    • 目标对象(Target Object):被一个或者多个切面(aspect)所通知(advise)的对象。也有人把它叫做被通知(advised)对象。 既然Spring AOP是通过运行时代理实现的,这个对象永远是一个 被代理(proxied)对象。
    • AOP代理(AOP Proxy):AOP框架创建的对象,用来实现切面契约(aspect contract)(包括通知方法执行等功能)。 在Spring中,AOP代理可以是JDK动态代理或者CGLIB代理。 注意:Spring 2.0最新引入的基于模式风格和@AspectJ注解风格的切面声明,对于使用这些风格的用户来说,代理的创建是透明的。
    • 织入(Weaving):把切面(aspect)连接到其它的应用程序类型或者对象上,并创建一个被通知(advised)的对象。 这些可以在编译时(例如使用AspectJ编译器),类加载时和运行时完成。 Spring和其他纯Java AOP框架一样,在运行时完成织入。

    通知的类型:

    • 前置通知(Before advice):在某连接点(join point)之前执行的通知,但这个通知不能阻止连接点前的执行(除非它抛出一个异常)。
    • 返回后通知(After returning advice):在某连接点(join point)正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。
    • 抛出异常后通知(After throwing advice):在方法抛出异常退出时执行的通知。
    • 后通知(After (finally) advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。
    • 环绕通知(Around Advice):包围一个连接点(join point)的通知,如方法调用。这是最强大的一种通知类型。环绕通知可以在方法调用前后完成自定义的行为。它也会选择是否继续执行连接点或直接返回它们自己的返回值或抛出异常来结束执行。

    环绕通知是最常用的通知类型。自从Spring AOP,如AspectJ,提供了完整的通知类型,我们推荐你使用能完成需要的功能的最适用的通知类型。例如,你仅仅需要将方法的返回值存储到缓存中去,那么你最好使用后置通知而不是环绕通知,尽管环绕通知可以实现同样的功能。使用最具体的通知类型提供了有着最少潜在错误的简单的程序模型。例如,你不需要使用在环绕通知中拦截proceed()方法,因此就不会出现拦截的错误。

    在Spring 2.0中,所有的通知参数都是静态(static)类型,因此你可以通过合适的类型(如方法返回值的类型)而不是Object数组来使用通知参数。

    连接点匹配切点的概念是AOP的核心,这与只提供拦截器的过时的技术有了很大的差别。切点能让通知独立的作用在面向对象的层次结构。例如,一个环绕通知提供了可以作用于多个对象(比如所有的业务层操作)的声明式事务管理。

    5.1.2 Spring AOP的功能和目标

    Spring AOP是基于简单Java实现的,不需要特殊的编译过程。Spring AOP不需要分层次的控制类加载器,因此这很适合作用于Servlet容器或者应用程序服务器。

    Spring AOP目前仅支持方法执行的连接点(对Spring Bean的方法执行进行通知)。属性拦截尽管可以不破坏Spring AOP APIs就添加到Spring AOP中去,但是还是没有去实现。如果你需要通知属性的访问与更新连接点,可以使用其他的AOP框架例如AspectJ。

    Spring AOP实现的AOP与其他AOP框架都不同。Spring AOP的目标不是提供最完整的AOP实现(尽管Spring AOP非常的强大),它更倾向于提供AOP实现与Spring IoC的紧密整合去解决在企业级开发的通用问题。

    因此,Spring AOP的功能常常与Spring IoC容器结合起来使用。切面就是通过普通的bean定义语法来进行配置的(尽管也可以使用强大的“自动代理(autoproxying)”),这是一个与其他AOP实现的关键差别。当然也有一些你不能简单高效的利用Spring AOP来完成的事,如对非常细粒度的对象通知(典型的例如域对象(domain objects)):AspectJ是在这类情况下最合适的选择。然而,我们的经验告诉我们Spring AOP为对AOP易控制的Java企业级开发提供了完美的解决方案。

    Spring AOP从来不力求与AspectJ竞争去提供一个完整的AOP解决方案。我们相信如Spring AOP一般基于代理的框架和如AspectJ一般成熟的框架都是有价值的,而且它们之间是互补的而不是互相竞争的。Spring利用AspectJ无缝整合Spring AOP和IoC。这种整合不会影响Spring AOP API或者AOP Alliance API:Spring AOP仍然向后兼容。后面的章节将会讲述Spring AOP API。

    • Spring框架的一个核心思想是“无侵入(non-invasiveness)”,这就是为什么你不应该强制将特定框架的接口和类引入业务或者域模型。然而,在某些地方Spring框架确实给你将Spring的依赖引入到你的代码中,Spring提供这样选项的原因是在某些特定的场合这将会使代码容易的多或者通过这样的方式写一些特定的代码。Spring总是给你提供选项:你可以自由地做出哪个选项是最适合你的特定情形的明智选择。
    • 与本章相关的一种选择是选择哪个AOP框架(以及哪种AOP形式)。你可以选择AspectJ和/或者Spring AOP,并且你也可以在基于@AspectJ注解的方式或者基于Spring XML配置的方式两者中选择一种进行开发。本章先介绍基于@AspectJ注解的开发方式并不是说Spring的开发团队更倾向于@AspectJ注解方式而不是Spring XML配置方式。
    • 你可以查看后续的章节来确定这两种方式的优缺点。

    5.1.3 AOP代理

    Spring AOP默认使用JDK动态代理作为AOP代理,这个可以对任何接口(或一系列接口)进行代理。

    Spring AOP也是用CGLIB动态代理,这对于对类进行代理而不是对接口进行代理是必须的。当业务对象没有实现接口时就会默认使用CGLIB动态代理。因为面向接口编程是一种良好的编程思想,所以业务类经常会去实现某些接口。当你需要通知没有在接口中声明的方法时,你也可以强制使用CGLIB动态代理。Spring AOP是基于代理的(proxy-based)是非常重要的。

    5.2 @AspectJ支持

    @AspectJ指的是通过在Java类上添加注解来声明切面的方式。AspectJ项目在AspectJ 5中介绍了@AspectJ注解方式开发。Spring使用了AspectJ 5中相同的注解,同时使用了提供AspectJ风格的切点表达式和匹配方式的类库。AOP运行仍然是纯Spring AOP,它不依赖于AspectJ编译器或织入器。

    • 使用AspectJ编译器和织入器就可以使用完整的AspectJ语言,这在后续章节有讨论。

    5.2.1 开启@AspectJ支持

    为了在Spring配置中使用@AspectJ切面你需要开启Spring AOP对@AspectJ切面的支持,并且对beans的自动代理取决于它们是否被这些切面通知。通过自动代理,如果Spring确定了一个bean被一个或多个切面通知,那么就会自动的为哪个bean生成一个代理去拦截方法调用并确保通知就像我们需要的那样执行。

    可以通过XML或者Java Confg的方式开启@AspectJ支持。不管使用哪种方式你都要确保你的在应用程序的classpath下有aspectjweaver.jar。这个类库可以在AspectJ的#39;lib#39; 目录下或者通过Maven仓库获取。

    通过Java Config开启@AspectJ支持

    通过在@Configuration下加入@EnableAspectJAutoProxy注解开启@AspectJ支持

    @Configuration

    @EnableAspectJAutoProxy

    public class AppConfig {

    }

    通过XML配置的方式开启@AspectJ支持

    通过XML配置中的aop:aspectj-autoproxy元素来开启@AspectJ支持

    lt;aop:aspectj-autoproxy/gt;

    5.2.2 声明一个切面

    开启了@AspectJ支持后,任何一个在你的应用程序上下文(applicationContext)中定义的AspectJ切面(有着 @Aspect注解的类)都可以被Spring自动检测到并且用于Spring AOP配置。下面的例子是一个没有多大用处的切面所需的最少的配置:

    在应用程序上下文(applicationContext)中定义的常规的bean,指向一个有着@AspectJ注解的类

    lt;bean id='myAspect' class='org.xyz.NotVeryUsefulAspect'gt;

    lt;!-- configure properties of aspect here as normal --gt;

    lt;/beangt;

    NotVeryUsefulAspect 定义如下,带有@Aspect注解

    package org.xyz;

    import org.aspectj.lang.annotation.Aspect;

    @Aspect

    public class NotVeryUsefulAspect {

    }

    切面(带有注解@

    全文共8826字,剩余内容已隐藏,支付完成后下载完整资料


    资料编号:[14769],资料为PDF文档或Word文档,PDF文档可免费转换为Word

原文和译文剩余内容已隐藏,您需要先支付 30元 才能查看原文和译文全部内容!立即支付

以上是毕业论文外文翻译,课题毕业论文、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。