Wednesday 7 October 2015

Implementing AOP with aspectJ

Step-1
                                      <!-- Using AOP with aspectJ -->
                   <dependency>
                             <groupId>org.aspectj</groupId>
                             <artifactId>aspectjrt</artifactId>
                             <version>1.6.11</version>
                   </dependency>

                   <dependency>
                             <groupId>org.aspectj</groupId>
                             <artifactId>aspectjweaver</artifactId>
                             <version>1.6.11</version>
                   </dependency>


Step-2
Enabling aspectJ annotation
apna-aop.xml

 <!-- We have concept for Proxy object for implementing AOP
         two kind of proxy
         1. Java Dynamic Proxy(proxy-target-class="false")
         2. CGLIB Proxy(proxy-target-class="true") 
     -->
        <!-- this will enable aspectJ annotation -->
   <aop:aspectj-autoproxy proxy-target-class="false"/>

 Step-3
 scan the package where advice class present  
   <!-- This is package name where our advice resides -->
   <context:component-scan base-package="com.apna.bank.aop"/>


Step-4
Define Advice using aspectJ annoation

@Aspect
@Component("apnaStartingTimeAdvice")
public class ApnaStartingTimeAdvice {

}

Step-5
define the point cut

Note: 
Pointcut: Pointcut are expressions that is matched with join points to determine whether advice needs to be executed or not. Pointcut uses different kinds of expressions that are matched with the join points and Spring framework uses the AspectJ pointcut expression language.

   @Before("execution(* com.apna.bank.dao.impl.*.*(..))")
   public void logBeforeTime(JoinPoint joinPoint) {
              System.out.println("Name of the method " + joinPoint.getSignature().getName());
             Date cdate=new Date();
             System.out.println("++++Current Date is  = "+cdate);
            System.out.println("___________________________________");
   }


@Before("execution(public String getName())")
public void getNameAdvice(){
        System.out.println("Executing Advice on getName()");
}
     

@Before("execution(* com.abc.spring.service.*.get*())")
public void getAllAdvice(){
      System.out.println("Service method getter called");
}


   Pointcut Methods and Reuse:-


//Pointcut to execute on all the methods of classes in a package
@Pointcut("within(com.abc.spring.service.*)")
public void allMethodsPointcut(){}


@Before("allMethodsPointcut()")
public void allServiceMethodsAdvice(){
        System.out.println("Before executing service method");
}


JoinPoint and Advice Arguments
@Aspect
public class EmployeeAspectJoinPoint {

 @Before("execution(public void com.spring.model..set*(*))")
  public void loggingAdvice(JoinPoint joinPoint){
  System.out.println("Before running loggingAdvice on method="+
joinPoint.toString());
       System.out.println("Agruments Passed="
Arrays.toString(joinPoint.getArgs()));
 
    }
     
    //Advice arguments, will be applied to bean methods with single String argument
    @Before("args(name)")
    public void logStringArguments(String name){
        System.out.println("String argument passed="+name);
    }
}

After Advice Example:-

@Aspect
public class EmployeeAfterAspect {
 
    @After("args(name)")
    public void logStringArguments(String name){
    System.out.println("Running After Advice. String argument
passed="+name);
    }
     
   @AfterThrowing("within(com.spring.model.Employee)")
    public void logExceptions(JoinPoint joinPoint){
       System.out.println("Exception thrown in Employee Method="+
joinPoint.toString());
    }
     
   @AfterReturning(pointcut="execution(* getName())"
returning="returnString")
    public void getNameReturningAdvice(String returnString){
        System.out.println("getNameReturningAdvice 
executed. Returned String="+returnString);
    }
     
}



Advice with Custom Annotation Pointcut :-

  Loggable.java

package com.spring.aspect;

public @interface Loggable {
 
}




package com.spring.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
 
@Aspect
public class EmployeeAnnotationAspect {
 
   @Before("@annotation(com.spring.aspect.Loggable)")
    public void myAdvice(){
        System.out.println("Executing myAdvice!!");
    }
}



@Repository
class CustomerDao implements ICustomerDao {

@Loggable
public void cool(){

}

}
















1 comment: