WELCOME Abdennour : Software engineer

Apr 5, 2012

AOP Spring Step By Step(PART3):"Vulgarizing" the AOP's concepts

1||*JoinPoint :
-A call to a method is a join point, and so is a field access.
-A for loop or an if statement is a join point, too.
-AspectJ exposes join points for a method call and a field access.
*PointCut :
-A pointcut is a program construct .
-it selects join points and collects join point context( =a join point has associated runtime information).

1)Selecting JoinPoint :

-it specify a selection criterion.

2)Collecting Join Point Context :

-Method call has the attached annotations of the method as the join point context.
-pointcuts can collect this context and pass it to advice.
-Pointcuts use patterns for these signatures to specify the selected join points.

-certain pointcut expressions aren’t relevant(pertinent) to proxy-based AOP.

Categorizing exposed join points
2||exposed join points :
  • perhaps a join point corresponding to a call or an exception handler in that method will do the job.
    categories of the join points exposed by AspectJ and their semantics.
    Category Exposed join point Code it represents
    3.a.Field access
    3.b.Field access
    4.aException processing
    Read access
    Write access
    Class initialization
    Object initialization
    Object pre-initialization
    Method body
    Method invocation
    Execution of an object’s creation logic
    Invocation of an object’s creation logic
    Access to read an object’s or a class’s field
    Access to write an object’s or a class’s field
    Catch block to handle an exception
    Class loading
    Object initialization in a constructor
    Object pre-initialization in a constructor
    Advice execution
1))JointPoint for Method :
1.a. method-execution join point :
$$public class Score{
$$ …...
$$public void add(double increase){
$$ if(this.prop+increase<=Score.MAX){
$$ this.prop=this.prop+increase;}
$$else {
$$ this.prop=Score.MAX ;
$$ }
=> method-execution join point encompasses the execution of all the code within the body of the method.
=>you can write advice for this join point to be applied before, after, and around the body.
=>the only join points available with Spring's proxy-based AOP.
1.b.method-call join point :
$$ Score score=new Score(200) ;
$$ score.add(300) ;
=>the call join point is the call to the add() method.
=>the code that forms the arguments isn’t part of the join point.(Example if we have : add(sum(5,5)) → sum(5,5)is not part of method call join point.
=>occurs at the places where a method is being invoked.
Difference between
method-execution join point  & method-call join point
the weaver weaves advice into the method body. the weaver weaves all method invocation
WHEN USE Method-Exexution JP or Method-Call JP ??
When use MEJP ? When Use MCJP ?
-If you want to weave the class which has the method t advice,
- to advise a method’s behavior
- If you want to affect only the caller classes
- you want tocollect the caller object’s context

2))JP for Constructors :
-it's When you need to apply crosscutting behavior at object-construction time.
2.a.Const-ExecJP :
$$ public class Score {
$$ …
$$ public Score(int prop){
$$ this.prop=prop ;
$$ }
=> the execution join point for the Score(int) constructor encompasses the entire constructor body.
2.b. Constructor-call join points :
represent the points that invoke the creation of an object.
$$ Score s=new Score(300) ;
3))Field Access JP :
-Used if :
  • need to perform some crosscutting action whenever a field is accessed.
  • need to ensure that a field contains the latest value before any code reads it.
  • want to mark an object dirty whenever a field in it is modified.
$$ public Account(int accountNumber) {
$$ this.accountNumber = accountNumber; <==== Write-Access JP
$$ }
$$ public String toString() {
$$ return "Account: "
$$ + accountNumber; <====Read-Access JP
$$ }

=>advsing field access join points doesn’t require implementing getters or setters.

4))Exception-handler join points :
=>concerning which is under catch{...}.
5))Class-initialization join points :
$$public class Score{
$$ static {
$$ …...
$$ ….........
$$ }

public class SavingsScore extends Score{
public SavingsAccount(int prop, boolean test) {
this.test=test; <================== ||
}                                                             ||
public SavingsAccount(int prop) { ||==>Object initialization join point
this(prop, false);                                     ||
this.miniIncrease = 40; <===========||

55))Object pre-initialization join points :
=>rarely used.
=>englobe the passage from the first called constructor to the beginning of its parent constructor.

66))Advice execution join points :
=>JP which encompasses the execution of any advice in the system.

-Trick :
=>Weave an aspect= Wrap it with a proxy Class.
-The Main rôle of Proxy :
=>intercepting advised method calls and forwarding those calls to the target bean.
-The proxied Object is create when the Application need the proxied bean.
-using an ApplicationContext, the proxied objects will be created when it loads all of the beans from the BeanFactory.
-Spring creates proxies at runtime( don’t need a special compiler to weave aspects)
- Because it’s based on dynamic proxies, Spring only supports
method join points.
  • without constructor pointcuts, there’s no way to apply advice when a bean is instantiated.

    => Spring aspects are implemented as proxies .
    =>This proxies wrap the target object.
    =>The proxy :
         1)handles method calls,
         2)performs additional aspect logic.
         3)invokes the target method.

    Selecting join points with pointcuts
    • pointcuts are defined using AspectJ’s pointcut expression language.
    • The pointcut may also need to collect join point context (objects such as this, arguments, and annotations) to provide to advice.
    • The advice uses the collected join point context in its implementation. (EXPLE : if the annotation specifies a
      read-only property, it may start a read-only transaction.)

      Hwo Writing pointcuts ?
      Execution ( * slm.Abdennour.fspringaop.Score.add( . . ) )
      Execution ( * slm.Abdennour.fspringaop.Score.add( . . ))
      Trigger on a method's execution Metho Specification
      Execution ( * slm.Abdennour.fspringaop.Score. add ( . .))

      Return any type Type that method belong to The method Any arguments
      Other Example (within):
      =execution ( * slm.Abdennour.fspringaop.Score.add(..)) && within(slm.Abdennour.fspringaop.*)
      ====>Execution of Score.add() when it's called from within any class in the slm.Abdennour.fspringaop.*

      (& is called ampersand)
      -In XML configuration ,We can use and instead of && ,likewise, Or instead of || .
      ==>Because ampersand have special meaning in XML cfg.

      Other Example (Bean):
      =execution ( * slm.Abdennour.fspringaop.Score.add(..)) && bean(pOne)
      ====>limit the Execution of Score.add() to the bean whose ID=pOne.
      Other Example ( !Bean):
      =execution ( * slm.Abdennour.fspringaop.Score.add(..)) && !bean(pOne)
      ====>limit the Execution of Score.add() to all beans that don't have ID=pOne.

1 comment: