`
yonlist
  • 浏览: 84486 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

AspectJ学习笔记 pointcuts

阅读更多

个人觉得PointcutsAspectJ学习的关键,文章搜集自网络,作者ah011

 

Pointcuts

public  pointcut  accountOperations call(*Account.*(..))

这是定义了一个pointcut,其中:

public access specifer

pointcut keyword

accountOperations poincut name

callpointcut typepointcut能与其他pointcut通过或(||)、与(&&)以及非(!)操作符联合

Accountsignature

 

常用pointcut类型

1) call
2) execution
3) target
4) args
5) within
6) cflow

 

基本概念

如果对上面的概念不是特别了解没有关系,下面就介绍一些基本的概念:

Joint Point

A join point is an identifiable point in the execution of a program. It could be a call to a method or an assignment to a member of an object.

join point 是程序执行过程中可以被识别的点。它可以是对一个函数的调用或是对象的一个属性。(注:springAOP只能做到对函数调用的拦截)

例子:

Public class Account

{

 void credit(float amount)

{

       _balance += amount;

}

}

这个例子中的join point包括Account类中执行credit()方法和对_balance的操作。

Pointcut

A pointcut is a program construct that selects join points and collects context at those points. For example, a pointcut can select a join point that is a call to a method, and it could also capture the method’s context, such as the target object on which the method was called and the method’s arguments.

pointcut 是一种程序结构,它用于选取join point并收集这些point的上下文信息。举例来说,pointcut可以是一个调用方法的join point,并且它能捕获这个方法的上下文信息,例如调用这个方法的目标对象和该方法的属性。

We can write a pointcut that will capture the execution of the credit() method in the Account class shown earlier:

我们可以写一个pointcut,它将可以捕获前面Account类中credit()方法的执行:

execution(void Account.credit(float))

To understand the difference between a join point and pointcut, think of pointcuts as specifying the weaving rules and join points as situations satisfying those rules.

要想理解join pointpointcut的不同,可以把pointcut想成明确了织入的规则,而join point则声明了符合这些规则的情况(即什么情况下运用织入的规则)。

笔记要点

1. In AspectJ, pointcut can be either anonymous or named.

aspectj中,poincut可以是匿名的,也可以是命名的。

 

2. Notice that the name of the pointcut is at the left of the colon and pointcut definition is at the right. The pointcut definition is syntax that identifies the join points where you want to insert some action.

注意pointcut的名字应位于冒号的左侧,而pointcut的定义在冒号的右侧。point cut的定义明确了你想在哪个join point插入一些动作。

 

3. Three wildcard notations are available in AspectJ:

1) * denotes any number of characters expect the period.
2).. denotes any number of characters including any number of period.
3) + denotes any subclass or subinerface of a given type.

以下是AspectJ中的三个通配符:

1)* 表示匹配任意数量的字符,除了句号(.)
2)..
表示比配包含句号在内的任意数量的字符。

3)+
表示比配任意给定的类型的子类和子接口。

 

Example:

*Account      

Types with a name ending with Account such as SavingsAccout and CheckingAccount

java.*.Data   

Types Data in any direct subpackages of the java package, such as java.util.Data and java.sql.Data.

java..*          

Any type inside the java package or all of its direct subpackage, such as java.awt and java.util, as well as indirect subpackages such as java.awt.event and java.util.logging

 

例:

*Account      

匹配以Account结尾的类型名,例如SavingsAccoutCheckingAccount

java.*.Data 

匹配java包下的直接子包下的Data类型,例如java.util.Datajava.sql.Data注:但不能是java.a.b.Data, 因为*不能包含句号嘛)

java..*          

匹配java包下任意类型或者其所有直接子包,例如java.awtjava.util,也可匹配其非直接子包,例如java.awt.eventjava.util.logging

 

4. Please note that in method signatures, the wildcard .. used to denote any type and number of arguments taken by a method.

请注意在method signatures中,通配符..被用于表示匹配包含任意类型和任意数量的参数的方法。

Example:

*java.io.Reader.read(char[],.. )

Any read() method in the Reader class irrespective of type and number of arguments to the method as long as the first argument type is char[]. In this case, it will mach read(char[])  and  read(char[], int, int), but not read().

例:

*java.io.Reader.read(char[],.. )

Reader类中的任何read()方法,无论参数是什么类型,数量如何,只要其首参数是char[]类型,就可以匹配。这种情况下,它可以匹配read(char[]),也可以匹配read(char[], int, int),但不能匹配read()

 

5.The execution join point is on the method body itself.

执行类型的join point是方法体本身。

Example:

In this code snippet, the point for the execution of the debit() method is the whole method body. This means that we can write advice for this join point to the applied before, after, and around the body.

在这个程序小片段里,debit()方法的执行类型joint point是整个方法体。这意味着我们可以为它写符合要求的advice(通知),包括beforeafteraround。(注:关于advice,将在下一篇中介绍)

 

6. The method call join point occurs at the place where this method is being invoked.

方法调用的join point放生在方法被引用的地方。

Example:

Account account = …...;

account.debit(100);  //debit() method call join point

Note that the code that forms the arguments is not a part of the join point. For example, if the debit() method is called in the statement account.debit(Currend.round(100.2345)), the call Current.round(100.2345) is not part of the debit() method call join point.

注意,代码形式的参数不是call join point的一部分。例如如果debit()方法以如下形式调用,account.debit(Currend.round(100.2345)),那么Current.round(100.2345)不是其method call join point的一部分

 

7. execution call join point 的区别

我的理解是:

1  在你想针对某个方法进行某些advice的织入时,应该用execution,即当想在某个方法的方法体之前,之后,之中织入某些advice时使用。

Example

在这段代码中,如果想在debit()的方法体,即

之前、之后、之中织入一些advice时使用。

2  call是想在调用某个方法的地方之前、之后织入某些advice时使用的。

Example

Account account = …...;

account.debit(100);  //即在句前、后加advice时使用。

 

8. Execution Object Pointcuts (thistarget)

1) These pointcuts match the join points based on the types of the objects at execution time.

这种pointcut用于在运行期匹配对象的类型。

2) In addition to matching the join points, these pointcuts are used to collect the context at the specified join point.

另外,这种pointcut可以用于收集join point附近的上下文信息。

3) The this() pointcut takes the form this(Type or ObjectIdentifier); it matches all join points that have a this object associated with them that is of the specified type or the specified ObjectIdentifier’s type. In other words, if you specify Type, it will match the join points where the expression this instanceof <Type> is true. The form of this pointcut that specifies ObjectIdentifier is used to collect the this object. If you need to match without collecting context, you will use the form that uses Type, but if you need to collect the context, you will use the form that uses ObjectIdentifier.

this() pointcut的基本形式为this(Type or ObjectIdentifier)它匹配所有有this对象的pointcut,而这些pointcut就是已声明的类型或对象标识符的类型。换句话说就是,如果你声明了Type,当表达式this instanceof <Type>为真时将匹配。声明为ObjectIdentifierpointcut将用于收集this对象的上下文信息。如果你想只匹配而不收集上下文,可以使用Type,反之则使用ObjectIdentifier

4) The target() pointcut is similar to the this () pointcut. The target() pointcut is normally used with a method call join point.

target() pointcut this() pointcut类似。target() pointcut 一般和call join point 搭配使用。

Example:

this(Account)       

All join points where this is instanceof Account. This will match all join points like methods calls and field assignments where the current execution object is Account, or its subclass, for example, SavingsAccount.

target(Account)    

All the join points where the object on which the method called is instanceof Account. This will match all join points where the target object is Account, or its subclass, for example, SavingsAccount.

target 匹配的是哪个对象调用了某方法

Example

ResgisterBuiness rb = new ResgisterBuiness();

forwardName = rb.doRegister(mapping.form);

对于doRegister方法来说,rb就是target

9. Lexical-structure based pointcuts

1) A lexical scope is a segment of source code. The within() pointcuts take the form of within(TypePattern) and are used to capture all the join points within the body of the specified classes and aspects, as well as any nested classes. The withincode() pointcuts take the form of either withincode(MethodSignature) or withincode(ConstrutorSignature) and are used to capture all the join points inside a lexical structure of constructor or a method, including any local classes in them.

lexical scope就是一个代码段。within() pointcut 形式是within(TypePattern) ,用来捕获声明的类、方面、还有内部类体的所有join pointwithincode() pointcut的形式是withincode(MethodSignature)withincode(ConstrutorSignature),用于捕获构造函数或方法的词法结构内的所有join point,包括它们内部的任何local classes

2) One common usage of the within() pointcut is to exclude the join points in the aspect itself. For example, the following pointcut excludes the join point corresponding to the calls to all print methods in the java.io.PrintStream class that occur inside the TraceAspect itself:

call(*java.io.PrintStream.print*(..)) && ! within(TraceAspect)

 

3) this within的作用差不多,以下为不同点:

<1> within包含内部类,但不包含子类

<2> this包含子类,但不包含内部类

 

10. Argument pointcuts

These pointcuts capture join points based on the argument type of a join point. Similar to execution object pointcuts these pointcuts can be used to capture the context.

Example:

args(String, .. , int)        All the join points in all methods where the first argument is of type String and last argument is of type int.

匹配所有以String为第一个参数类型,以int为最后一个参数类型的方法。

 

la

分享到:
评论

相关推荐

    Spring AspectJ的学习一

    **Spring AspectJ 学习详解** 在Java世界中,Spring框架以其强大的依赖注入(DI)和面向切面编程(AOP)能力而闻名。AspectJ是AOP领域的一个强大工具,它扩展了Java语言,允许开发者创建所谓的"切面",来封装横切...

    aspectj-1.9.6.jar

    《AspectJ 1.9.6.jar:Java AOP编程的基石》 AspectJ是Java平台上的一种面向切面编程(AOP)框架,它允许开发者将关注点分离,提高代码的可维护性和可复用性。AspectJ 1.9.6.jar文件是AspectJ库的核心组成部分,它...

    book :aspectj in action

    在这本书中,作者深入浅出地阐述了AspectJ的基本概念和核心特性,包括切面、通知(advice)、连接点(join points)、切入点(pointcuts)以及引入(introductions)。他们通过丰富的实例和实际应用来展示如何利用...

    Aspectj5学习文档

    AspectJ是Java编程语言的一个面向切面编程(AOP)的框架,它是基于Java语言的,能够给Java程序添加切面编程的特性。AspectJ5是AspectJ的一个版本,它引入了许多新的特性和改进,这些改进包括对Java 5的支持、对...

    精通AspectJ_源代码

    在学习AspectJ时,有几个关键概念是必须掌握的: 1. **切面(Aspect)**:切面是AspectJ的核心,它封装了系统中的一个关注点,例如数据验证、事务管理等。切面由通知(advice)、切点(pointcut)和引入...

    Aspectj

    同时,提供的《AspectJ程序设计指南.pdf》也是一份详细的教程,涵盖了AspectJ的各个方面,值得深入阅读和学习。 通过理解和掌握AspectJ,开发者能够更有效地组织代码,提升代码质量和可维护性。对于大型复杂项目,...

    spring aop 学习笔记

    本学习笔记将深入探讨Spring AOP的核心概念、工作原理以及实际应用。 1. **核心概念** - **切面(Aspect)**:切面是关注点的模块化,包含业务逻辑之外的横切关注点,如日志、事务管理。 - **连接点(Join Point...

    aspectj-1.7.0.jar

    aspectj-1.7.0.jar aspectj的包

    aspectJ

    学习AspectJ,你需要理解其基本概念,并掌握如何定义和使用切面。同时,熟悉AspectJ的语法和工具,如ajc编译器和AspectJ Weaver,能让你在实际项目中更好地应用面向方面的编程。最后,实践是检验理论的最好方式,...

    aspectj.pdf

    Pointcuts(切入点) 切入点是一组连接点的集合,用于指定哪些连接点上应用切面逻辑。切入点设计者允许使用逻辑运算符(&&, ||, !)来组合多个条件,并且可以从切入点传递参数到通知(Advice)中。 #### 3. Advice...

    spring AspectJ aop学习

    当我们谈论"spring AspectJ aop学习"时,我们将深入探讨Spring AOP如何结合AspectJ来实现更灵活的模块化和解耦。 首先,让我们理解AOP的概念。面向切面编程(Aspect Oriented Programming)是一种编程范式,旨在将...

    org.aspectj,aspectj项目库(org.aspectj).zip

    这个压缩包可能包含了AspectJ的完整源代码,方便开发者进行学习、调试和定制。 【描述】中的"建筑物"可能是指构建项目或构建过程,暗示了这个库不仅包含源代码,还可能包括构建脚本和依赖关系,使得开发者能够从...

    aspectj-1.6.9.jar

    AspectJ 是一个强大的面向切面编程(AOP)的框架,它允许开发者在Java应用程序中分离关注点,将横切逻辑(如日志、事务管理、安全性等)与核心业务逻辑相分离。`aspectj-1.6.9.jar` 是AspectJ框架的一个特定版本,即...

    征服Spring AOP—— @AspectJ

    使用@AspectJ,我们可以创建一个包含切面逻辑的类,并通过注解来声明切点(pointcuts)和通知(advisors)。下面是一些关键的@AspectJ注解: 1. `@Aspect`:标记一个类为切面类,此类包含切点和通知。 2. `@...

    The AspectJ Programming Guide.2003

    #### 切点(Pointcuts) 切点是AspectJ中用来选择连接点的表达式。它们可以基于类型、方法签名或其他条件来匹配连接点,从而决定在何处插入通知。 #### 通知(Advice) 通知是在切点所选择的连接点处执行的代码。...

    AspectJ的JAR包

    AspectJ是一种强大的面向切面编程(AOP)框架,它扩展了Java语言,允许开发者在不改变原有代码结构的情况下,插入关注点代码。在Spring AOP框架中,AspectJ被广泛用于实现更精细粒度的切面逻辑,提供比Spring默认的...

    Aspectj1.6的jar包

    AspectJ是Java平台上的一个强大的面向切面编程(AOP)框架,它允许开发者在不修改源代码的情况下,对程序进行横切关注点的插入,如日志、事务管理、性能监控等。AspectJ 1.6是该框架的一个较早版本,但仍然被广泛...

    aspectj jar

    AspectJ和Spring的整合在Java开发中扮演着重要的角色,特别是在实现面向切面编程(AOP)时。AspectJ是一个强大的、成熟的库,它扩展了Java语言,允许开发者定义和执行切面,这是一种分离关注点的方式,使得业务逻辑...

    Aop之AspectJ详解解读demo

    **Aop之AspectJ详解解读** 在软件开发中,面向切面编程(AOP)是一种设计模式,它允许程序员将关注点分离到不同的模块,从而提高代码的可维护性和复用性。AspectJ是Java平台上的一个开源AOP框架,它提供了一种强大...

Global site tag (gtag.js) - Google Analytics