Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

SP AspectJ Annotation


The Spring Framework recommends you to use Spring AspectJ AOP implementation over the Spring 1.2 old style dtd based AOP implementation because it provides you more control and it is easy to use.

There are two ways to use Spring AOP AspectJ implementation:

  1. By annotation: We are going to learn it here.
  2. By xml configuration (schema based): We will learn it in next page.

To understand the aop concepts, its advantage etc. visit here AOP Concepts Tutorial


download all examples (developed using MyEclipse IDE)

Spring AspectJ AOP implementation provides many annotations:

  1. @Aspect declares the class as aspect.
  2. @Pointcut declares the pointcut expression.

The annotations used to create advices are given below:

  1. @Before declares the before advice. It is applied before calling the actual method.
  2. @After declares the after advice. It is applied after calling the actual method and before returning result.
  3. @AfterReturning declares the after returning advice. It is applied after calling the actual method and before returning result. But you can get the result value in the advice.
  4. @Around declares the around advice. It is applied before and after calling the actual method.
  5. @AfterThrowing declares the throws advice. It is applied if actual method throws exception.

Understanding Pointcut

Pointcut is an expression language of Spring AOP.

The @Pointcut annotation is used to define the pointcut. We can refer the pointcut expression by name also. Let’s see the simple example of pointcut expression.

  1. @Pointcut(“execution(* Operation.*(..))”)
  2. private void doSomething() {}

The name of the pointcut expression is doSomething(). It will be applied on all the methods of Operation class regardless of return type.

Understanding Pointcut Expressions

Let’s try the understand the pointcut expressions by the examples given below:

  1. @Pointcut(“execution(public * *(..))”)

It will be applied on all the public methods.


  1. @Pointcut(“execution(public Operation.*(..))”)

It will be applied on all the public methods of Operation class.


  1. @Pointcut(“execution(* Operation.*(..))”)

It will be applied on all the methods of Operation class.


  1. @Pointcut(“execution(public Employee.set*(..))”)

It will be applied on all the public setter methods of Employee class.


  1. @Pointcut(“execution(int Operation.*(..))”)

It will be applied on all the methods of Operation class that returns int value.


1) @Before Example

The AspectJ Before Advice is applied before the actual business logic method. You can perform any operation here such as conversion, authentication etc.

Create a class that contains actual business logic.

File: Operation.java

  1. package com.Kreationnext;
  2. public  class Operation{
  3.     public void msg(){System.out.println(“msg method invoked”);}
  4.     public int m(){System.out.println(“m method invoked”);return 2;}
  5.     public int k(){System.out.println(“k method invoked”);return 3;}
  6. }

Now, create the aspect class that contains before advice.

File: TrackOperation.java

  1. package com.Kreationnext;
  2. import org.aspectj.lang.JoinPoint;
  3. import org.aspectj.lang.annotation.Aspect;
  4. import org.aspectj.lang.annotation.Before;
  5. import org.aspectj.lang.annotation.Pointcut;
  6. @Aspect
  7. public class TrackOperation{
  8.     @Pointcut(“execution(* Operation.*(..))”)
  9.     public void k(){}//pointcut name
  10.     @Before(“k()”)//applying pointcut on before advice
  11.     public void myadvice(JoinPoint jp)//it is advice (before advice)
  12.     {
  13.         System.out.println(“additional concern”);
  14.         //System.out.println(“Method Signature: ”  + jp.getSignature());
  15.     }
  16. }

Now create the applicationContext.xml file that defines beans.

File: applicationContext.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4.     xmlns:aop=“http://www.springframework.org/schema/aop”
  5.        xsi:schemaLocation=”http://www.springframework.org/schema/beans 
  6.        http://www.springframework.org/schema/beans/spring-beans.xsd 
  7.        http://www.springframework.org/schema/aop 
  8.        http://www.springframework.org/schema/aop/spring-aop.xsd”>
  9.     <bean id=“opBean” class=“com.Kreationnext.Operation”>   </bean>
  10.     <bean id=“trackMyBean” class=“com.Kreationnext.TrackOperation”></bean>
  11.     <bean class=“org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator”></bean>
  12. </beans>

Now, let’s call the actual method.

File: Test.java

  1. package com.Kreationnext;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Test{
  5.     public static void main(String[] args){
  6.         ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);
  7.         Operation e = (Operation) context.getBean(“opBean”);
  8.         System.out.println(“calling msg…”);
  9.         e.msg();
  10.         System.out.println(“calling m…”);
  11.         e.m();
  12.         System.out.println(“calling k…”);
  13.         e.k();
  14.     }
  15. }

Output

  1. calling msg…
  2. additional concern
  3. msg() method invoked
  4. calling m…
  5. additional concern
  6. m() method invoked
  7. calling k…
  8. additional concern
  9. k() method invoked

As you can see, additional concern is printed before msg(), m() and k() method is invoked.

Now if you change the pointcut expression as given below:

  1. @Pointcut(“execution(* Operation.m*(..))”)

Now additional concern will be applied for the methods starting with m in Operation class. Output will be as this:

  1. calling msg…
  2. additional concern
  3. msg() method invoked
  4. calling m…
  5. additional concern
  6. m() method invoked
  7. calling k…
  8. k() method invoked

Now you can see additional concern is not printed before k() method invoked.


2) @After Example

The AspectJ after advice is applied after calling the actual business logic methods. It can be used to maintain log, security, notification etc.

Here, We are assuming that Operation.java, applicationContext.xml and Test.java files are same as given in @Before example.

Create the aspect class that contains after advice.

File: TrackOperation.java

  1. package com.Kreationnext;
  2. import org.aspectj.lang.JoinPoint;
  3. import org.aspectj.lang.annotation.Aspect;
  4. import org.aspectj.lang.annotation.After;
  5. import org.aspectj.lang.annotation.Pointcut;
  6. @Aspect
  7. public class TrackOperation{
  8.     @Pointcut(“execution(* Operation.*(..))”)
  9.     public void k(){}//pointcut name
  10.     @After(“k()”)//applying pointcut on after advice
  11.     public void myadvice(JoinPoint jp)//it is advice (after advice)
  12.     {
  13.         System.out.println(“additional concern”);
  14.         //System.out.println(“Method Signature: ”  + jp.getSignature());
  15.     }
  16. }

Output

  1. calling msg…
  2. msg() method invoked
  3. additional concern
  4. calling m…
  5. m() method invoked
  6. additional concern
  7. calling k…
  8. k() method invoked
  9. additional concern

You can see that additional concern is printed after calling msg(), m() and k() methods.


3) @AfterReturning Example

By using after returning advice, we can get the result in the advice.

Create the class that contains business logic.

File: Operation.java

  1. package com.Kreationnext;
  2. public  class Operation{
  3.     public int m(){System.out.println(“m() method invoked”);return 2;}
  4.     public int k(){System.out.println(“k() method invoked”);return 3;}
  5. }

Create the aspect class that contains after returning advice.

File: TrackOperation.java

  1. package com.Kreationnext;
  2. import org.aspectj.lang.JoinPoint;
  3. import org.aspectj.lang.annotation.AfterReturning;
  4. import org.aspectj.lang.annotation.Aspect;
  5. @Aspect
  6. public class TrackOperation{
  7.     @AfterReturning(
  8.               pointcut = “execution(* Operation.*(..))”,
  9.               returning= “result”)
  10.     public void myadvice(JoinPoint jp,Object result)//it is advice (after returning advice)
  11.     {
  12.         System.out.println(“additional concern”);
  13.         System.out.println(“Method Signature: “  + jp.getSignature());
  14.         System.out.println(“Result in advice: “+result);
  15.         System.out.println(“end of after returning advice…”);
  16.     }
  17. }

File: applicationContext.xml

It is same as given in @Before advice example

File: Test.java

Now create the Test class that calls the actual methods.

  1. package com.Kreationnext;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Test{
  5.     public static void main(String[] args){
  6.         ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);
  7.         Operation e = (Operation) context.getBean(“opBean”);
  8.         System.out.println(“calling m…”);
  9.         System.out.println(e.m());
  10.         System.out.println(“calling k…”);
  11.         System.out.println(e.k());
  12.     }
  13. }

Output

  1. calling m…
  2. m() method invoked
  3. additional concern
  4. Method Signature: int com.Kreationnext.Operation.m()
  5. Result in advice: 2
  6. end of after returning advice…
  7. 2
  8. calling k…
  9. k() method invoked
  10. additional concern
  11. Method Signature: int com.Kreationnext.Operation.k()
  12. Result in advice: 3
  13. end of after returning advice…
  14. 3

You can see that return value is printed two times, one is printed by TrackOperation class and second by Test class.


4) @Around Example

The AspectJ around advice is applied before and after calling the actual business logic methods.

Here, we are assuming that applicationContext.xml file is same as given in @Before example.

Create a class that contains actual business logic.

File: Operation.java

  1. package com.Kreationnext;
  2. public  class Operation{
  3.     public void msg(){System.out.println(“msg() is invoked”);}
  4.     public void display(){System.out.println(“display() is invoked”);}
  5. }

Create the aspect class that contains around advice.

You need to pass the PreceedingJoinPoint reference in the advice method, so that we can proceed the request by calling the proceed() method.

File: TrackOperation.java

  1. package com.Kreationnext;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.Around;
  4. import org.aspectj.lang.annotation.Aspect;
  5. import org.aspectj.lang.annotation.Pointcut;
  6. @Aspect
  7. public class TrackOperation
  8. {
  9.     @Pointcut(“execution(* Operation.*(..))”)
  10.     public void abcPointcut(){}
  11.     @Around(“abcPointcut()”)
  12.     public Object myadvice(ProceedingJoinPoint pjp) throws Throwable
  13.     {
  14.         System.out.println(“Additional Concern Before calling actual method”);
  15.         Object obj=pjp.proceed();
  16.         System.out.println(“Additional Concern After calling actual method”);
  17.         return obj;
  18.     }
  19. }

File: Test.java

Now create the Test class that calls the actual methods.

  1. package com.Kreationnext;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Test{
  5.     public static void main(String[] args){
  6.         ApplicationContext context = new classPathXmlApplicationContext(“applicationContext.xml”);
  7.         Operation op = (Operation) context.getBean(“opBean”);
  8.         op.msg();
  9.         op.display();
  10.     }
  11. }

Output

  1. Additional Concern Before calling actual method
  2. msg() is invoked
  3. Additional Concern After calling actual method
  4. Additional Concern Before calling actual method
  5. display() is invoked
  6. Additional Concern After calling actual method

You can see that additional concern is printed before and after calling msg() and display methods.


5) @AfterThrowing Example

By using after throwing advice, we can print the exception in the TrackOperation class. Let’s see the example of AspectJ AfterThrowing advice.

Create the class that contains business logic.

File: Operation.java

  1. package com.Kreationnext;
  2. public  class Operation{
  3.     public void validate(int age)throws Exception{
  4.     if(age<18){
  5.         throw new ArithmeticException(“Not valid age”);
  6.     }
  7.     else{
  8.         System.out.println(“Thanks for vote”);
  9.     }
  10.     }
  11. }

Create the aspect class that contains after throwing advice.

Here, we need to pass the Throwable reference also, so that we can intercept the exception here.

File: TrackOperation.java

  1. package com.Kreationnext;
  2. import org.aspectj.lang.JoinPoint;
  3. import org.aspectj.lang.annotation.AfterThrowing;
  4. import org.aspectj.lang.annotation.Aspect;
  5. @Aspect
  6. public class TrackOperation{
  7.     @AfterThrowing(
  8.               pointcut = “execution(* Operation.*(..))”,
  9.               throwing= “error”)
  10.     public void myadvice(JoinPoint jp,Throwable error)//it is advice
  11.     {
  12.         System.out.println(“additional concern”);
  13.         System.out.println(“Method Signature: “  + jp.getSignature());
  14.         System.out.println(“Exception is: “+error);
  15.         System.out.println(“end of after throwing advice…”);
  16.     }
  17. }

File: applicationContext.xml

It is same as given in @Before advice example

File: Test.java

Now create the Test class that calls the actual methods.

  1. package com.Kreationnext;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Test{
  5.     public static void main(String[] args){
  6.         ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);
  7.         Operation op = (Operation) context.getBean(“opBean”);
  8.         System.out.println(“calling validate…”);
  9.         try{
  10.             op.validate(19);
  11.         }catch(Exception e){System.out.println(e);}
  12.         System.out.println(“calling validate again…”);
  13.         try{
  14.             op.validate(11);
  15.         }catch(Exception e){System.out.println(e);}
  16.     }
  17. }

Output

  1. calling validate…
  2. Thanks for vote
  3. calling validate again…
  4. additional concern
  5. Method Signature: void com.Kreationnext.Operation.validate(int)
  6. Exception is: java.lang.ArithmeticException: Not valid age
  7. end of after throwing advice…
  8. java.lang.ArithmeticException: Not valid age

Comments

comments