登录

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

注册

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

找回密码

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

燃气表运行监管系统之租户信息管理的设计与实现外文翻译资料

 2022-08-24 11:08  

Spring AOP APIs

7.1.

The previous chapter described the Springrsquo;s support for AOP with @AspectJ and schema-based aspect definitions. In this chapter, we discuss the lower-level Spring AOP APIs. For common applications, we recommend the use of Spring AOP with AspectJ pointcuts as described in the previous chapter.

7.2. Pointcut API in Spring

This section describes how Spring handles the crucial pointcut concept.

7.2.1. Concepts

Springrsquo;s pointcut model enables pointcut reuse independent of advice types. You can target different advice with 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 follows:

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 are matched. The following listing shows the ClassFilter interface definition:

public interface ClassFilter {

boolean matches(Class clazz);

}

The MethodMatcher interface is normally more important. The complete interface follows:

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 ever matches 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 two-argument matches method returns true for a given method, and the isRuntime() method for the MethodMatcher returns true, the three-argument matches method is invoked on every method invocation. This lets a pointcut look at the arguments passed to the method invocation immediately before the target advice is to execute.

Most MethodMatcher implementations are static, meaning that their isRuntime() method returns false. In this case, the three-argument matches method is never 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.

7.2.2. Operations on Pointcuts

Spring supports operations (notably, union and intersection) on pointcuts.

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

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

7.2.4. Convenience Pointcut Implementations

Spring provides several convenient pointcut implementations. You can use some of them directly. Others are intended to be subclassed in application-specific pointcuts.

Static Pointcuts

Static pointcuts are based on the method and the target class and cannot take into account the methodrsquo;s arguments. Static pointcuts sufficethinsp;—thinsp;and are bestthinsp;—thinsp;for most usages. Spring can 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.

The rest of this section describes some of the static pointcut implementations that are 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 that uses the regular expression support in the JDK.

With the JdkRegexpMethodPointcut class, you can provide a list of pattern strings. If any of these is a match, the pointcut evaluates to true. (So, the result is effectively the union of these pointcuts.)

The following example shows how to use JdkRegexpMethodPointcut:

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 named RegexpMethodPointcutAdvisor, which lets us also reference an Advice (remember that an Advice can be an interceptor, before advice, throws advice, and others). Behind the scenes, Spring uses a JdkRegexpMethodPointcut. Using RegexpMethodPointcutAdvisor simplifies wiring, as the one bean encapsulates both pointcut and advice, as the following example shows:

lt;bean id='settersAndAbsquatulateAdvisor'

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

lt;property name='advice'gt;

lt;ref bean='beanNameOfAop

剩余内容已隐藏,支付完成后下载完整资料


Chapter7. Spring AOP APIs

7.1.简介

前一章介绍了Spring 2.0中提供的使用@AspectJ和基于Schema切面定义的AOP。在这个章节里,我们将讨论更底层的Spring AOP API, 以及如何在Spring 1.2应用中使用这些API。对于新的应用程序,我们推荐使用前一章介绍的Spring 2.0 AOP支持,但是当你使用已有系统时, 或是阅读书籍和文章时,很有可能会遇到Spring 1.2风格的例子。Spring 2.0是完全向前兼容Spring 1.2的, 这一章中涉及的所有内容在Spring 2.0里面得到了完全的支持。

7.2.Spring中的切入点API

让我们看看Spring是如何处理切入点这个重要概念的。

7.2.1.概念

Spring的切入点模型使得切入点可以独立于通知(advice)类型进行重用,这就使得针对不同通知使用相同的pointcut成为可能。

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

}

matches(Method, Class)方法用来测试这个切入点是否匹配目标类的指定方法。 这将在AOP代理被创建的时候进行运算,这样可以避免在每次方法调用的时候都运算。 如果matches(Method, Class)对于一个给定的方法返回true,并且isRuntime()也返回true, 那么matches(Method, Class, Object[])将在每个方法调用的时候被调用。 这使得切入点在通知将被执行前可以查看传入到方法的参数。

大多数MethodMatcher是静态的,这意味着isRuntime()方法返回false。 在这种情况下,matches(Method, Class , Object[])永远不会被调用。

Tip

应尽可能地使切入点保持静态,这就允许AOP框架在AOP代理被创建时缓存对切入点的计算结果。

7.2.2.切入点运算

Spring在切入点上支持以下运算: 或和与。

或运算表示只需有一个切入点被匹配就执行方法

与运算表示所有的切入点都匹配的情况下才执行。

通常或运算要更有用一些。

切入点可以使用org.springframework.aop.support.Pointcuts类中的静态方法来进行组合, 或者使用同一个包内的ComposablePointcut类。不过使用AspectJ切入点表达式通常会更简单一些。

7.2.3.AspectJ切入点表达式

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

可以查看前一章关于所支持的AspectJ切入点原语的讨论。

7.2.4.便利的切入点实现

Spring提供了一些很方便的切入点实现。一些可直接使用,其它的则是切入点应用规范的子集。

7.2.4.1.静态切入点

静态切入点基于方法和目标类进行切入点判断,不考虑方法的参数。在多数情况下,静态切入点是高效的、最好的选择。 Spring只在第一次调用方法时运算静态切入点:以后每次调用这个方法时就不需要再运算了。

让我们考虑Spring中的一些静态切入点实现。

7.2.4.1.1.正则表达式切入点

显而易见,一种描述静态切入点的方式是使用正则表达式。包含Spring在内的一些AOP框架都支持这种方式。 org.springframework.aop.support.Perl5RegexpMethodPointcut是一个最基本的正则表达式切入点, 它使用Perl 5正则表达式语法。Perl5RegexpMethodPointcut依赖Jakarta ORO进行正则表达式匹配。 Spring也提供了JdkRegexpMethodPointcut类,它使用JDK 1.4或更高版本里提供的正则表达式支持。

使用Perl5RegexpMethodPointcut类,你可以提供一组模式字符串。 如果其中任意一个模式匹配,切入点将被解析为true。(实际上就是这些切入点的或集。)

用法如下:

lt;bean id='settersAndAbsquatulatePointcut'

class='org.springframework.aop.support.Perl5RegexpMethodPointcut'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, 它也允许我们引用一个通知(记住这里一个通知可以是拦截器,前置通知(before advice),异常通知(throws advice)等类型中的一个)。 在背后,如果使用J2SE 1.4或者以上版本,Spring将使用JdkRegexpMethodPointcut, 在之前版本的虚拟机上,Spring将退回去使用Perl5RegexpMethodPointcut。 可以通过设置perl5属性为true来强制使用Perl5RegexpMethodPointcut。 使用RegexpMethodPointcutAdvisor可以简化织入,因为一个bean可以同时作为切入点和通知器(advisor),如下所示:

lt;bean id='settersAndAbsquatulateAdvisor'

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

lt;property name='advice'gt;

lt;ref local='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可以和任何通知类型一起使用

7.2.4.1.2.属性驱动的切入点

一个重要的静态切入点是元数据驱动(metadata-driven)切入点。这使用元数据参数:特别是源代码级别的元数据。

7.2.4.2.动态切入点

动态切入点比起静态切入点在执行时要消耗更多的资源。它们同时计算方法参数和静态信息。 这意味着它们必须在每次方法调用时都被计算;由于参数的不同,结果不能被缓存。

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

7.2.4.2.1.控制流切入点

Spring控制流切入点在概念上和AspectJ的cflow 切入点很相似, 虽然它的功能不如后者那么强大。(目前还不能让一个切入点在另外一个切入点所匹配的连接点处执行)。 一个控制流切入点匹配当前的调用栈。例如,一个连接点被com.mycompany.web 包内的一个方法或者SomeCaller类调用,切入点就可能被激活。 控制流切入点是由org.springframework.aop.support.ControlFlowPointcut类声明的。

Note

在运行时控制流切入点的开销是非常昂贵的,甚至与其它动态切入点比起来也是如此。在Java 1.4里, 它的开销差不多是其它动态切入点的5倍。

7.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和更高版本中的任意通知类型。

剩余内容已隐藏,支付完成后下载完整资料


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

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

企业微信

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