登录

  • 登录
  • 忘记密码?点击找回

注册

  • 获取手机验证码 60
  • 注册

找回密码

  • 获取手机验证码60
  • 找回
毕业论文网 > 外文翻译 > 计算机类 > 计算机科学与技术 > 正文

Spring AOP APIs外文翻译资料

 2022-07-21 03:07  

Spring AOP APIs

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.

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

1.1 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.

1.2 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.

1.3 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.

1.4 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 JDK 1.4 .

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

1.5 Attribute-driven pointcuts

An important type of static pointcut is a metadata-driven pointcut. This uses the values of metadata attributes: typically, source-level metadata.

1.6 Dynamic pointcuts

Dynamic pointcuts are costlier to evaluate than static pointcuts. They take into account method arguments, as well as static information. This means that they must be evaluated with every method invocation; the result cannot be cached, as arguments will vary.

The main example is the control flow pointcut.

1.7 Control flow pointcuts

Spring control flow pointcuts are conceptually similar to AspectJ cflow pointcuts, although less powerful. (There is currently no way to specify that a pointcut executes below a join point matched by another pointcut.) A control flow pointcut matches the current call stack. For example, it might fire if the join point was invoked by a method in thecom.mycompany.web package, or by the SomeCaller class. Control flow pointcuts are specified using theorg.springframework.aop.support.ControlFlowPointcut class.

2 Advice API in Spring

2.1 Advice lifecycles

Each advice is a Spring bean. An advice instance can be shared across all advised objects, or unique to each advised object. This corresponds to per-class or per-instanceadvice.

Per-class advice is used most often. It is appropriate for generic advice such as transaction advisors. These do not depend on the state of the proxied object or add new state; they merely act on the method and arguments.

Per-instance advice is appropriate for introductions, to support mixins. In this case, the advice adds state to the proxied object.

Itrsquo;s possible to use a mix of shared and per-instance advice in the same AOP proxy.

2.2 Advice types in Spring

Spring provides several advice types out of the box, and is extensible to support arbitrary advice types. Let us look at the basic concepts and standard advice types.

2.2.1 Interception around advice

The most fundamental advice type in Spring is interception around advice.Spring is compliant with the AOP Alliance interface for around advice using method interception.

Note the call to the MethodInvocationrsquo;s proceed() method. This proceeds down the interceptor chain towards the join point. Most interceptors will invoke this method, and return its return value. However, a MethodInterceptor, like any around advice, can return a different value or throw an exception rather than invoke the proceed method. However, you donrsquo;t want to do this without good reason!

MethodInterceptors offer interoperability with other AOP Alliance-compliant AOP implementations. The other advice types discussed in the remainder of this section implement common AOP concepts, but in a Spring-specific way. While there is an advantage in using the most specific advice type, stick with MethodInterceptor around advice if you are likely to want to run the aspect in another AOP framework. Note that pointcuts are not currently interoperable between frameworks, and the AOP Alliance does not currently define pointcut interfaces.

2.2.2 Before advice

A simpler advice type is a before advice. This does not need a MethodInvocation object, since it will only be ca

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


Spring AOP APIs

1概念

Spring的切入点模型支持切入点重用独立类型的通知。这是使用相同的切入点可能针对不同的通知。

org.springframework.aop.Pointcut接口是中央的接口,用于目标通知特定的类和方法。

如果可能的话,当创建一个AOP代理时尽量让切入点静态,允许缓存的AOP框架切入点的结果评估。

1.1切入点操作

Spring支持操作的切入点:值得注意的是,联盟和相交。

联盟意味着要么切入点匹配方法。

交叉意味的切入点匹配方法。

联盟通常更有用。

切入点可以由在 org使用静态方法。Spring AOP支持的切入点的这些类,或使用 composablepointcut 类在同一个包。 然而,使用AspectJ切入点表达式通常是一个简单的方法。

1.2方便的切入点实现

Spring提供了一些方便的切入点实现。有些可以用框;别人的目的是定义在特定应用的切入点。

1.3静态的切入点

静态的切入点是基于方法和目标类,,不能考虑 方法的参数。静态的切入点是充分的和最好的——对于大多数用途。Spring来评估一个静态的切入点可能只有一次,当一个方法调用:在那之后,不需要再次评估切入点与每个方法调用。

1.4正则表达式的切入点

一个显而易见的方式来指定静态切入点是正则表达式。一些AOP 框架除了Spring使这一切成为可能。org.springframework.aop.support.JdkRegexpMethodPointcut是一个通用的规则切入点表达式,使用正则表达式支持JDK 1.4 。

使用JdkRegexpMethodPointcut类,您可以提供一个模式字符串的列表。 如果这些是一个匹配,切入点将评估为true。 (所以结果是有效的结合这些切入点。)

1.5 Attribute-driven的切入点

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

1.6动态的切入点

动态的切入点比起静态的切入点来说是昂贵的。他们考虑到方法参数以及静态信息。这意味着他们必须评估每个方法调用,结果不能被缓存,而且作为参数有所不同的。

主要的例子是control flow切入点。

1.7控制流的切入点

Spring的AspectJ切入点在概念上类似于控制流cflow切入点, 虽然不那么强大。(目前还没有指定执行切入点下面一个连接点匹配另一个切入点)。一个控制流切入点匹配当前调用堆栈。例如,也许如果连接点是由一个方法调用在com.mycompany.web包,或SomeCaller类,那么它可能会燃烧。控制流的切入点指定使用类org.springframework.aop.support.ControlFlowPointcut

2 Spring中的Advice API

2.1 Advice的生命周期

每一个advice都是Spring bean。一个实例可以共享所有advice的advice对象,或独特的每个advice的对象。这对应于每个类或每个advice。

每个类最常使用的advice。它是适合通用的advice等事务顾问。这些不依赖于状态的代理对象或添加新的状态;他们只是在方法和参数。

介绍每个advice是适当的,以支持mixin。在这种情况下, advice增加状态的代理对象。

可以使用的共享和每个advice在同一AOP代理。

2.2 Spring中的Advice类型

Spring提供了几个advice类型的盒子,并可支持任意的advice类型。 让我们看看基本概念和标准advice的类型。

2.2.1环绕拦截advice

Spring里最基本的advice是环绕拦截advice。Spring是符合AOP联盟界面在使用方法的advice拦截。

注意调用的方法调用的 proceed() 方法。 这个收益下降拦截器链向的连接点。最大的拦截器将调用此方法,并返回它的返回值。然而,像methodinterceptor,任何advice一样,可以返回不同的值或抛出一个异常,而不是调用的处理方法。然而,你不想做这件事没有很好的理由!

methodinterceptors提供与其他AOP联盟标准的AOP实现互操作性。本节中讨论的其他意见类型实现共同的AOP概念,但在一个Spring的具体途径。 虽然是以最具体的advice类型的优势,坚持methodinterceptor advice如果你可能想在另一个AOP框架运行方面。 注意的切入点是目前不可互操作的框架之间,和AOP联盟目前不确定切入点接口。

2.2.2 Before advice

类型是一个更简单的advice之前的advice。这个不需要MethodInvocation对象,因为它只会被称为之前进入的方法。

Before advice的主要优势是不需要调用proceed()方法,因此不可能无意中未能继续下拦截器链。

2.2.3 抛出的advice

抛出的advice返回后调用连接点如果连接点抛出一个例外。 Spring提供了输入抛出的advice。注意,这意味着org.springframework.aop.ThrowsAdvice接口不包含任何方法:这是一个标签界面识别给定对象实现的一个或多个输入抛出advice的方法。 这些是应该有的形式:

afterThrowing(方法、参数、目标,subclassOfThrowable)

只有最后一个参数是必需的。方法签名可以有一个或四个参数,取决于感兴趣的方法和advice方法参数。

如果一个throws-advice方法抛出一个异常,它会覆盖原始异常(即改变异常,扔给用户)。 压倒一切的异常通常是一个RuntimeException;这是兼容的任何方法签名。然而,如果一个throws-advice检查方法抛出一个异常,它会匹配的声明异常目标方法,因此在某种程度上耦合到特定目标方法签名。不抛出未申报检查异常不兼容的目标方法的签名! advice可用于任何切入点。

2.3 JavaBean属性

和大多数一样FactoryBean实现提供了Spring,ProxyFactoryBean类本身就是一个JavaBean。 它的属性是用来:

一些关键属性继承org.springframework.aop.framework.ProxyConfig(在Spring所有AOP代理工厂的超类)。 这些主要特性包括:

  • proxyTargetClass: true如果目标类代理,而不是目标类的接口。 如果设置了这个属性值true,然后CGLIB代理将被创建(但也看到部分11.5.3,“JDK和CGLIB-based代理”)。
  • optimize:控制是否积极优化应用于代理通过创建CGLIB。人们不应轻率地使用这个设置,除非一个完全了解有关AOP代理处理优化。这是目前使用的仅为CGLIB代理;它使用JDK的动态代理没有影响。
  • frozen:如果一个代理配置frozen,然后更改配置不再允许。这是有用的作为一个轻微的优化和对这些情况下,当你不希望用户能够操纵代理(通过劝接口)后的代理已经被创建。此属性的默认值是false,所以变化如增加额外的advice是被允许的。exposeProxy:决定是否当前代理应该暴露ThreadLocal这样它就可以被访问的目标。如果一个目标需要获得代理和exposeProxy属性设置为true,目标可以使用AopContext.currentProxy()方法。

其他属性特定于ProxyFactoryBean包括:

  • proxyInterfaces:字符串数组接口名称。 如果这不是提供,CGLIB 目标类的代理将使用(但也看到部分11.5.3,“JDK和CGLIB-based代理”)。
  • interceptorNames:字符串数组Advisor名字,拦截器或其他advice 适用。顺序很重要,先来先过。也就是说列表中的第一个拦截器将会是第一个能够拦截调用。

名字是bean名称在当前工厂,包括来自祖先的bean的名称工厂。 你不能在这里提及bean引用,因为这样做会导致ProxyFactoryBean忽略了独立设置的advice。

你可以用*(附加一个拦截器名称*)。这将导致应用程序的所有顾问bean名字开始前的星号的部分应用,使用这个特性的一个例子可以找到11.5.6节“使用全球顾问”。

  • 单例模式:工厂是否应该返回一个对象,无论如何通常getObject()方法被调用。几个FactoryBean实现提供这样的一个方法。默认值是true。 ——如果您想要使用有状态的advice示例中,状态混合,使用原型advice连同一个单值false。

2.4 JDK- and CGLIB-based代理

这部分作为明确的文档如何ProxyFactoryBean选择创建一个JDK之一——和CGLIB-based代表一个特定的目标对象(也就是代理)。

如果类的目标对象是代理(以下称为目标类)没有实现任何接口,然后CGLIB-based代理创建。这是最简单的场景中,因为JDK代理是基于接口的,没有接口意味着JDK代理是不可能实现的。一个简单的目标bean中插头, 并指定拦截器通过列表interceptorNames财产。 请注意, CGLIB-based代理将创建即使proxyTargetClass财产的ProxyFactoryBean被设置为false。 (这显然毫无意义,是从bean中最好的定义,因为它是有最好的冗余,并在最坏的情况下混乱。)

如果目标类实现一个(或多个)接口,然后代理的类型就取决于的配置创建ProxyFactoryBean。

如果proxyTargetClass财产的ProxyFactoryBean被设置为true, 然后CGLIB-based代理将被创建。这是有意义的,与是一致的最小惊讶原则。 即使proxyInterfaces财产的ProxyFactoryBean已将一个或多个完全限定的接口名称,事实呢这一proxyTargetClass属性设置为true 将导致CGLIB-based代理的效果。

如果proxyInterfaces财产的ProxyFactoryBean已将一个或多个完全限定的接口名称通过JDK-based代理将被创建。创建的代理将实现的所有接口中指定proxyInterfaces财产;如果目标类实现一个接口比这些指定的proxyInterfaces财产都很不错,但这些不会实现的额外接口返回代理。 全文共7326字,剩余内容已隐藏,支付完成后下载完整资料


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

您需要先支付 30元 才能查看全部内容!立即支付

企业微信

Copyright © 2010-2022 毕业论文网 站点地图