Spring AOP AspectJ Annotation Configuration Example

AspectJ libraries provides the facility to declare aspect, pointcut etc with the help of annotations. Let us discuss the commonly used AspectJ annotations first.

Declaring an aspect:

The @Aspect annotation is used to declare a class as an aspect.

@Aspect
public class AspectModule {
 
}

Declaring a pointcut:

The @Pointcut annotation is used to declare a pointcut. The expression parameter represents the expression used for matching the join point.

@Pointcut("execution(expression)")
private void businessService() {
	//Block of code.
}

Declaring advices:

The @{ADVICE-NAME} annotations is used to declare an advice.

Spring AOP AspectJ Annotation Configuration Advice Example:

BusinessLogic.java

/**
 * This class will be used as a bean class.
 * @author w3spoint
 */
public class BusinessLogic {
	public void implementBusinessLogic(){
	  System.out.println("Business logic executed.");
	}
 
	public String returnValue(){
	  System.out.println("Return value executed.");
	  return "w3spoint.com";
	}
 
	public void throwException(){
	  System.out.println("Business logic throw exception.");
	  throw new ArithmeticException("Arithmetic exception occure.");
	}
}

AdviceTest.java

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
 
@Aspect
public class AdviceTest {  
 
@Pointcut("execution(* com.w3spoint.business.*.*(..))")
private void selectAll(){
 
}
 
@Before("selectAll()")
public void beforeAdvice(){
     System.out.println("Before advice executed.");
}
 
@After("selectAll()")
public void afterAdvice(){
    System.out.println("After advice executed.");
}
 
@AfterReturning(pointcut = "selectAll()", returning="retVal")
public void afterReturningAdvice(Object retVal){
    System.out.println("After returning advice executed.");
    System.out.println("Returning value: " + retVal);
}
 
@AfterThrowing(pointcut = "selectAll()", throwing = "ex")
public void afterThrowingAdvice(ArithmeticException ex){
  System.out.println("Throwing advice executed.");
  System.out.println("Exception: " + ex.getMessage());   
}
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
 
     <aop:config>
      <aop:aspect id="log" ref="adviceTest">
         <aop:pointcut id="selectAll" 
         expression="execution(* com.w3spoint.business.*.*(..))"/>
         <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
         <aop:after pointcut-ref="selectAll" method="afterAdvice"/>
         <aop:after-returning pointcut-ref="selectAll" 
                              returning="retVal"
                              method="afterReturningAdvice"/>
         <aop:after-throwing pointcut-ref="selectAll" 
                             throwing="ex"
                             method="afterThrowingAdvice"/>
      </aop:aspect>
   </aop:config>
 
   <bean id="businessLogic" 
    class="com.w3spoint.business.BusinessLogic"/>
   <bean id="adviceTest" 
    class="com.w3spoint.business.AdviceTest"/>
 
</beans>

Test.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Test {
 public static void main(String[] args) {
  //Get ApplicationContext using spring configuration file.
  ApplicationContext context = 
    new ClassPathXmlApplicationContext("applicationContext.xml");
 
  //Get BusinessLogic bean object from ApplicationContext instance. 
  BusinessLogic businessLogic = 
    (BusinessLogic) context.getBean("businessLogic");
 
  //Call implementBusinessLogic method of BusinessLogic bean.
  businessLogic.implementBusinessLogic();
  System.out.println("------------------------------------");
  businessLogic.returnValue();
  System.out.println("------------------------------------");
  businessLogic.throwException();
 } 
}

Output:

Before advice executed.
Business logic executed.
After advice executed.
After returning advice executed.
Returning value: null
-----------------------------------------
Before advice executed.
Return value executed.
After advice executed.
After returning advice executed.
Returning value: w3spoint.com
-----------------------------------------
Before advice executed.
Business logic throw exception.
After advice executed.
Throwing advice executed.
Exception: Arithmetic exception occure.
Exception in thread "main" java.lang.ArithmeticException: Arithmetic exception occure.
	at com.w3spoint.business.BusinessLogic.throwException(BusinessLogic.java:19)
	at com.w3spoint.business.BusinessLogic$$FastClassByCGLIB$$19ab96db.invoke(<generated>)
	at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
	at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:692)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
	at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:42)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625)
	at com.w3spoint.business.BusinessLogic$$EnhancerByCGLIB$$4299115d.throwException(<generated>)
	at com.w3spoint.business.Test.main(Test.java:20)

  Download this example.  

Please follow and like us:
Content Protection by DMCA.com