Spring Framework Documentation外文翻译资料

 2022-03-31 08:03

Spring Framework Documentation

6. Spring AOP APIs

6.1. Introduction

The previous chapter described the Springrsquo;s support for AOP using @AspectJ and schema-based aspect definitions. In this chapter we discuss the lower-level Spring AOP APIs and the AOP support typically used in Spring 1.2 applications. For new applications, we recommend the use of the Spring 2.0 and later AOP support described in the previous chapter, but when working with existing applications, or when reading books and articles, you may come across Spring 1.2 style examples. Spring 5 remains backwards compatible with Spring 1.2 and everything described in this chapter is fully supported in Spring 5.

6.2. Pointcut API in Spring

Letrsquo;s look at how Spring handles the crucial pointcut concept.

6.2.1. Concepts

Springrsquo;s pointcut model enables pointcut reuse independent of advice types. Itrsquo;s possible to target different advice using the same pointcut.

The org.springframework.aop.Pointcut interface is the central interface, used to target advices to particular classes and methods. The complete interface is shown below:

public interface Pointcut {

ClassFilter getClassFilter();

MethodMatcher getMethodMatcher();

}

Splitting the Pointcut interface into two parts allows reuse of class and method matching parts, and fine-grained composition operations (such as performing a 'union' with another method matcher).

The ClassFilter interface is used to restrict the pointcut to a given set of target classes. If the matches() method always returns true, all target classes will be matched:

public interface ClassFilter {

boolean matches(Class clazz);

}

The MethodMatcher interface is normally more important. The complete interface is shown below:

public interface MethodMatcher {

boolean matches(Method m, Class targetClass);

boolean isRuntime();

boolean matches(Method m, Class targetClass, Object[] args);

}

The matches(Method, Class) method is used to test whether this pointcut will ever match a given method on a target class. This evaluation can be performed when an AOP proxy is created, to avoid the need for a test on every method invocation. If the 2-argument matches method returns true for a given method, and the isRuntime() method for the MethodMatcher returns true, the 3-argument matches method will be invoked on every method invocation. This enables a pointcut to look at the arguments passed to the method invocation immediately before the target advice is to execute.

Most MethodMatchers are static, meaning that their isRuntime() method returns false. In this case, the 3-argument matches method will never be invoked.

If possible, try to make pointcuts static, allowing the AOP framework to cache the results of pointcut evaluation when an AOP proxy is created.

6.2.2. Operations on pointcuts

Spring supports operations on pointcuts: notably, union and intersection.

  • Union means the methods that either pointcut matches.
  • Intersection means the methods that both pointcuts match.
  • Union is usually more useful.
  • Pointcuts can be composed using the static methods in the org.springframework.aop.support.Pointcuts class, or using the ComposablePointcut class in the same package. However, using AspectJ pointcut expressions is usually a simpler approach.

6.2.3. AspectJ expression pointcuts

Since 2.0, the most important type of pointcut used by Spring is org.springframework.aop.aspectj.AspectJExpressionPointcut. This is a pointcut that uses an AspectJ supplied library to parse an AspectJ pointcut expression string.

See the previous chapter for a discussion of supported AspectJ pointcut primitives.

6.2.4. Convenience pointcut implementations

Spring provides several convenient pointcut implementations. Some can be used out of the box; others are intended to be subclassed in application-specific pointcuts.

Static pointcuts

Static pointcuts are based on method and target class, and cannot take into account the methodrsquo;s arguments. Static pointcuts are sufficient - and best - for most usages. Itrsquo;s possible for Spring to evaluate a static pointcut only once, when a method is first invoked: after that, there is no need to evaluate the pointcut again with each method invocation.

Letrsquo;s consider some static pointcut implementations included with Spring.

Regular expression pointcuts

One obvious way to specify static pointcuts is regular expressions. Several AOP frameworks besides Spring make this possible. org.springframework.aop.support.JdkRegexpMethodPointcut is a generic regular expression pointcut, using the regular expression support in the JDK.

Using the JdkRegexpMethodPointcut class, you can provide a list of pattern Strings. If any of these is a match, the pointcut will evaluate to true. (So the result is effectively the union of these pointcuts.)

The usage is shown below:

lt;bean id='settersAndAbsquatulatePointcut'

class='org.springframework.aop.support.JdkRegexpMethodPointcut'gt;

lt;property name='patterns'gt;

lt;listgt;

lt;valuegt;.*set.*lt;/valuegt;

lt;valuegt;.*absquatulatelt;/valuegt;

lt;/listgt;

lt;/propertygt;

lt;/beangt;

Spring provides a convenience class, RegexpMethodPointcutAdvisor, that allows us to also reference an Advice (remember that an Advice can be an interceptor, before advice, throws advice etc.). Behind the scenes, Spring will use a JdkRegexpMethodPointcut. Using RegexpMethodPointcutAdvisor simplifies wiring, as the one bean encapsulates both pointcut and advice, as shown below:

lt;bean id='settersAndAbsquatulateAdvisor' 全文共30416字,剩余内容已隐藏,支付完成后下载完整资料


Spring Framework Documentation

6. Spring AOP APIs

6.1. 介绍

上一章介绍了Spring使用@AspectJ和基于模式的方面定义对AOP的支持。 在本章中,我们将讨论Spring 1.2应用程序中通常使用的较低级别的Spring AOP API和AOP支持。 对于新的应用程序,我们推荐使用前一章中介绍的Spring 2.0及更高版本的AOP支持,但是在使用现有应用程序或阅读书籍和文章时,您可能会遇到Spring 1.2样式的示例。 Spring 5保持向后兼容Spring 1.2,并且Spring 5完全支持本章描述的所有内容。

6.2. Spring中的Pointcut API

我们来看看Spring如何处理关键的切入点概念。

6.2.1. 概念

Spring的切入点模型使切入点可以独立于通知类型进行重用。 使用相同的切入点可以针对不同的建议。

org.springframework.aop.Pointcut接口是中央接口,用于将通知提供给特定的类和方法。 完整的接口如下所示:

public interface Pointcut {

ClassFilter getClassFilter();

MethodMatcher getMethodMatcher();

}

将Pointcut接口分割成两部分允许重用类和方法匹配部分,以及细粒度的组合操作(例如与另一个方法匹配器执行“联合”)。

ClassFilter接口用于将切入点限制为给定的一组目标类。 如果matches()方法总是返回true,则所有目标类都将匹配:

public interface ClassFilter {

boolean matches(Class clazz);

}

MethodMatcher接口通常更重要。 完整的接口如下所示:

public interface MethodMatcher {

boolean matches(Method m, Class targetClass);

boolean isRuntime();

boolean matches(Method m, Class targetClass, Object[] args);

}

匹配(Method,Class)方法用于测试此切入点是否曾经匹配目标类上的给定方法。 在创建AOP代理时可以执行此评估,以避免需要对每个方法调用进行测试。 如果对于给定方法,2参数匹配方法返回true,并且MethodMatcher的isRuntime()方法返回true,则将在每个方法调用时调用3参数匹配方法。 这使得切入点可以在目标通知执行之前立即查看传递给方法调用的参数。

大多数MethodMatchers是静态的,意味着它们的isRuntime()方法返回false。 在这种情况下,三个参数的匹配方法将永远不会被调用。

如果可能,请尝试使切入点为静态,从而允许AOP框架在创建AOP代理时缓存切入点评估的结果。

6.2.2. 切入点中的操作

Spring支持切入点操作:特别是联合和交集。
联合意味着切入点匹配的方法。
交叉点意味着两个切入点匹配的方法。
联合通常更有用。
切入点可以使用org.springframework.aop.support.Pointcuts类中的静态方法进行组合,也可以使用同一个包中的ComposablePointcut类进行组合。 但是,使用AspectJ切入点表达式通常是一种更简单的方法。

6.2.3. AspectJ切入点表达式

从2.0开始,Spring使用的最重要的切入点类型是org.springframework.aop.aspectj.AspectJExpressionPointcut。 这是一个使用AspectJ提供的库来解析AspectJ切入点表达式字符串的切入点。

有关支持的AspectJ切入点基元的讨论,请参阅上一章。

6.2.4. 方便的切入点实现

Spring提供了几个方便的切入点实现。有些可以在盒子外面使用;其他的打算在特定于应用程序的切入点中进行子类化。
静态切入点

静态切入点基于方法和目标类,并且不能考虑方法的参数。对于大多数用途来说,静态切入点已经足够,而且是最好的。当一个方法被第一次调用时,Spring可能只对一个静态切入点进行一次评估:在这之后,不需要再用每个方法调用来评估切入点。

让我们考虑Spring中包含的一些静态切入点实现。
正则表达式切入点

指定静态切入点的一种显而易见的方式是正则表达式。除Spring之外的几个AOP框架使这成为可能。

org.springframework.aop.support.JdkRegexpMethodPointcut是一个通用的正则表达式切入点,使用JDK中的正则表达式支持。
使用JdkRegexpMethodPointcut类,可以提供模式字符串列表。如果其中任何一个匹配,则切入点将评估为true。 (所以结果就是这些切入点的有效结合。)

用法如下所示:

lt;bean id='settersAndAbsquatulatePointcut'

class='org.springframework.aop.support.JdkRegexpMethodPointcut'gt;

lt;property name='patterns'gt;

lt;listgt;

lt;valuegt;.*set.*lt;/valuegt;

lt;valuegt;.*absquatulatelt;/valuegt;

lt;/listgt;

lt;/propertygt;

lt;/beangt;

Spring提供了一个方便的类RegexpMethodPointcutAdvisor,它允许我们也引用一个Advice(请记住,Advice可以是一个拦截器,在建议之前,抛出建议等)。 在幕后,Spring将使用JdkRegexpMethodPointcut。 使用RegexpMethodPointcutAdvisor简化了连线,因为一个bean封装了切入点和通知,如下所示:

lt;bean id='settersAndAbsquatulateAdvisor'

class='org.springframework.aop.support.RegexpMethodPointcutAdvisor'gt;

lt;property name='advice'gt;

lt;ref bean='beanNameOfAopAllianceInterceptor'/gt;

lt;/propertygt;

lt;property name='patterns'gt;

lt;listgt;

lt;valuegt;.*set.*lt;/valuegt;

lt;valuegt;.*absquatulatelt;/valuegt;

lt;/listgt;

lt;/propertygt;

lt;/beangt;

RegexpMethodPointcutAdvisor可以用于任何建议类型。

属性驱动的切入点

一个重要的静态切入点类型是一个元数据驱动的切入点。这使用元数据属性的值:通常是源级别元数据。

动态切入点

动态切入点比静态切入点更昂贵。它们考虑了方法参数以及静态信息。这意味着必须使用每个方法调用来评估它们;结果不能被缓存,因为参数会有所不同。

主要的例子是控制流切入点。

控制流切入点

Spring控制流切入点在概念上与AspectJ cflow切入点类似,但功能较弱。 (目前没有办法指定切入点在由另一个切入点匹配的连接点下执行)。控制流切入点匹配当前调用栈。例如,如果连接点由com.mycompany.web包中的方法或SomeCaller类调用,则它可能会触发。控制流切入点使用org.springframework.aop.support.ControlFlowPointcut类指定。

在运行时评估控制流切入点比其他动态切入点要昂贵得多。 在Java 1.4中,成本约为其他动态切入点的5倍。

6.2.5切入点超类

Spring提供了有用的切入点超类来帮助你实现自己的切入点。

因为静态切入点非常有用,所以您可能继承了StaticMethodMatcherPointcut的子类,如下所示。 这需要实现一个抽象方法(尽管可以重写其他方法来自定义行为):

class TestStaticPointcut extends StaticMethodMatcherPointcut {

public boolean matches(Method m, Class targetClass) {

// return true if custom criteria match

}

}

还有动态切入点的超类。

您可以在Spring 1.0 RC2及更高版本中使用任何通知类型的自定义切入点。

6.2.6自定义切入点

因为Spring AOP中的切入点是Java类,而不是语言特性(如AspectJ中的),所以可以声明自定义切入点,无论是静态切入点还是动态切入点。 Spring中的自定义切入点可以任意复杂。 但是,如果可能,建议使用AspectJ切入点表达式语言。

Spring的后续版本可以提供对JAC提供的“语义切入点”的支持:例如,“更改目标对象中的实例变量的所有方法”。

6.3. Spring中的通知API

现在我们来看看Spring AOP如何处理通知。

6.3.1. 通知的生命周期

每个通知是一个Spring bean。 通知实例可以在所有通知的对象上共享,或者对每个通知的对象都是唯一的。 这对应于每类或每个实例的通知。

类通知最常使用。 适用于交易顾问等通用通知。 这些不依赖于代理对象的状态或添加新的状态; 他们只是在方法和论据上采取行动。

每个实例的通知适合引入,以支持mixin。 在这种情况下,通知将状态添加到代理对象。

可以在同一个AOP代理中混合使用共享和每个实例的通知。

6.3.2 Spring中的通知类型

Spring提供了几种开箱即用的通知类型,并且可以扩展以支持任意的通知类型。 让我们看看基本概念和标准通知类型。

Interception Around 通知

Spring中最基本的通知类型是Interception Around

Spring使用方法拦截来满足AOP Alliance界面的要求。 实施周围通知的MethodInterceptors应该实现以下接口:

public interface MethodInterceptor extends Interceptor {

Object invoke(MethodInvocation invocation) throws Throwable;

}

invoke()方法的MethodInvocation参数公开了正在调用的方法; 目标连接点; AOP代理; 以及该方法的参数。 invoke()方法应该返回调用的结果:连接点的返回值。

一个简单的MethodInterceptor实现如下所示:

public class DebugInterceptor implements MethodInterceptor {

public Object invoke(MethodInvocation invocation) throws Throwable {

System.out.println('Before: invocation=[' invocation ']');

Object rval = invocation.proceed();

System.out.println('Invocation returned');

return rval;

}

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


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

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

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