Wednesday, May 17, 2017

Spring AOP

Introduction

Spring AOP (Aspect-oriented programming) framework helps to decouple cross-cutting concerns form the objects that they affect.Using Spring framework cross-cutting concerns can now be modularized into special objects called "Aspects". This concept helps to reduce boilder plate codes and helps to implement separation of concerns. Transactions, Security, Logging are some examples of cross-cutting concerns.

Aspect

This can be define as a centralized module which has a set of APIs providing cross-cutting concerns across the application.

Aspect = advice + pointcut;

Advice

Aspects have a purpose- a job that they are meant to do.In AOP the job of an aspect is called "advice". Advice defines both "what" and "when" of an aspect.
"what" : what is the job  "when" : when to perform job. Should it apply before or after method invoke?

joinpoint

Your application may have thousands of opportunities for advice to be applied.These opportunities are known "joinpoints". So a joinpoint is a point in the execution of the application where an aspect can be plugged in.

pointcut

A subset of joinpoint actually needs to be advised by an aspect. If advice defines "what" and "when" of aspects then pointcut defines the "where". Typically they are defined within Advice annotations.
i.e

@Before("execution(void doSomething())")

pointcut examples 

"execution(void doSomething())" => apply on exactly "void doSomething()" method
"execution(* doSomething())" => apply on "doSomething()" regardless return type
"execution(* doSomething(..))" => apply on "doSomething" regardless of return type and any number of parameters and any type.
"execution(* doSomething(*))" => apply on "doSomething" regardless of return type but exactly one argument of any type.
"execution(* com.rahal..*Service.*(..))" => execute any method, class name ends with Service in package com.rahal or sub packages.
"execution(* *..*Service.*(..))" => execute any method, class name ends with Service in any package or sub packages.
"execution(@annotation.Trace * *(..))" => Method must be annotated. Note annotation.Trace is fully qualified name of the annotation.
 "execution(* (@annotation.Trace *).*(..))" => Class must be annotated.
 "execution(void doSomething(..) || void doThisThing())" => you can use OR, AND operators

Code example

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.apache.log4j.Logger;

@Component // define as a spring bean
@Aspect    // define this bean as aspect
public class TracingAspect {
   
    boolean enteringCalled = false;
    private final Logger log = Logger.getLogger(this.getClass());
   
    public boolean isEnteringCalled(){
        return enteringCalled;
    }
   
    /*
     * Follwing method is an example of Aspect. Method body defines "what"
     * and @Before define "when" to execute Aspect. "execution(void doSomething())"
     * define the pointcut("where" to apply)
     */
    @Before("execution(void doSomething())")
    public void entering(JoinPoint joinPoint){
        enteringCalled = true;
        log.trace("***************** entering " + joinPoint.getStaticPart().toString());
    }
}

Target

A target is the object that is being adviced. Without AOP this object would have to contain
its primary logic + cross-cutting concerns. With AOP traget object is free to focus on its primary logic.

Proxy


A proxy is the object created after applying advice to the target object. As far as outside client objects are
concerned , the target object (pre-AOP) and the proxy object(post-AOP) are the same .

In Spring AOP, 4 type of advices are supported :

1.Before advice – Run before the method execution
2.After advice – Run after the method exection
3.After returning advice – Run after the method returns a result successfully
4.After throwing advice – Run after the method throws an exception
5.Around advice – Run around the method execution, combine all advices above. Following are the special characteristics about this advice.
    i. Can prevent original method from calling
    ii. Can catch the exception
    iii. Can modify return value

No comments:

Post a Comment