AspectJ

Aspect-oriented programming先参考这篇文章:

http://blog.csdn.net/onlyqi/article/details/7168050


参考wiki:

http://en.wikipedia.org/wiki/AspectJ

AspectJ Programming Guide:

http://www.eclipse.org/aspectj/doc/released/progguide/index.html


All valid Java programs are also valid AspectJ programs, but AspectJ also allows programmers to define special constructs called aspects.

AspectJ是Aspect-oriented programming在java中的扩展,所以任何标准的java代码都是AspectJ代码,但是AspectJ代码比标准java代码扩展出了以下部分:


Aspects can contain several entities unavailable to standard classes. These are:

    inter-type declarations—allow a programmer to add methods, fields, or interfaces to existing classes from within the aspect. This example adds an acceptVisitor (see visitor pattern) method to the Point class:

aspect VisitAspect {
  void Point.acceptVisitor(Visitor v) {
    v.visit(this);
  }
}

    pointcuts — allow a programmer to specify join points (well-defined moments in the execution of a program, like method call, object instantiation, or variable access). All pointcuts are expressions (quantifications) that determine whether a given join point matches. For example, this point-cut matches the execution of any instance method in an object of type Point whose name begins with set:

pointcut set() : execution(* set*(..) ) && this(Point);

    advice — allows a programmer to specify code to run at a join point matched by a pointcut. The actions can be performed before, after, or around the specified join point. Here, the advice refreshes the display every time something on Point is set, using the pointcut declared above:

after () : set() {
  Display.update();
}

AspectJ also supports limited forms of pointcut-based static checking and aspect reuse (by inheritance).


其中最关键的就是join points,也就是pointcut,即定义在什么地方做cross-cutting。而AspectJ对此的语法是:


AspectJ's join-point model

    The join points in AspectJ include method or constructor call or execution, the initialization of a class or object, field read and write access, exception handlers, etc. They do not include loops, super calls, throws clauses, multiple statements, etc.

    Pointcuts are specified by combinations of primitive pointcut designators (PCDs).

1,  "Kinded" PCDs match a particular kind of join point (e.g., method execution) and tend to take as input a Java-like signature. One such pointcut looks like this:

 execution(* set*(*))

    This pointcut matches a method-execution join point, if the method name starts with "set" and there is exactly one argument of any type.

2, "Dynamic" PCDs check runtime types and bind variables. For example

  this(Point)

    This pointcut matches when the currently-executing object is an instance of class Point. Note that the unqualified name of a class can be used via Java's normal type lookup.

3, "Scope" PCDs limit the lexical scope of the join point. For example:

 within(com.company.*)

    This pointcut matches any join point in any type in the com.company package. The * is one form of the wildcards that can be used to match many things with one signature.

4, Pointcuts can be composed and named for reuse. For example

   pointcut set() : execution(* set*(*) ) && this(Point) && within(com.company.*);

    This pointcut matches a method-execution join point, if the method name starts with "set" and this is an instance of type Point in the com.company package. It can be referred to using the name "set()".

 

Advice specifies to run at (before, after, or around) a join point (specified with a pointcut) certain code (specified like code in a method). The AOP runtime invokes Advice automatically when the pointcut matches the join point.

For example:

after() : set() {
   Display.update();
}

    This effectively specifies: "if the set() pointcut matches the join point, run the code Display.update() after the join point completes." 


要想在java中使用AspectJ,我们需要准备两样东西:

1,由于AspectJ使用的不同于java的语法,我们需要用AspectJ complier。eclipse的AspectJ development tool已经集成了这个编译器。

2,AspectJ的jar包。

官网上都提供:
http://eclipse.org/aspectj/







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值