Spring AOP AspectJ Xml Configuration Example

AspectJ libraries provides the facility to declare aspect, pointcut etc using xml file. Let us discuss the syntax first.

Declaring an aspect:

The element is used to declare an aspect. The ref attribute is used for bean reference.

Declaring a pointcut:

The element is used to declare an pointcut. The expression element represents the expression used for matching the join point.

Declaring advices:

The element is used to declare an advice.

Advice types:

Before:
It executes before the method execution.

After:
It executes after the method execution. It not depends upon the method outcome.

after-returning:
It executes after the method execution when method completes successfully.

after-throwing:
It executes after the method execution when method exits by throwing an exception.

Around:
It executes before and after the advised method is called.

Spring AOP AspectJ Xml Configuration Before Advice Example:

BusinessLogic.java

/**
 * This class will be used as a bean class.
 * @author javawithease
 */
public class BusinessLogic {
	public void implementBusinessLogic(){
		System.out.println("Business logic executed.");
	}
}

BeforeAdviceTest.java

import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
 
public class BeforeAdviceTest implements MethodBeforeAdvice{  
    @Override  
    public void before(Method method, Object[] args, 
    		Object target)throws Throwable {  
        System.out.println("Additional concern " +
        		"before business logic.");  
    }  
}

applicationContext.java

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
   <bean id="businessLogic" 
   		class="com.javawithease.business.BusinessLogic"/>
   <bean id="beforeAdviceTest" 
   		class="com.javawithease.business.BeforeAdviceTest"/>
   <bean id="proxy" 
   		class="org.springframework.aop.framework.ProxyFactoryBean">
	<property name="target" ref="businessLogic"></property>  
	<property name="interceptorNames">  
	 <list>  
	  <value>beforeAdviceTest</value>  
	 </list>  
    </property> 
   </bean> 
</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("proxy", BusinessLogic.class);
 
  //Call implementBusinessLogic method of BusinessLogic bean.
  businessLogic.implementBusinessLogic();
 } 
}

Output:

Additional concern before business logic.
Business logic executed.

 
Download this example.

Spring AOP AspectJ Xml Configuration after-returning Advice Example:

BusinessLogic.java

/**
 * This class will be used as a bean class.
 * @author javawithease
 */
public class BusinessLogic {
	public void implementBusinessLogic(){
		System.out.println("Business logic executed.");
	}
}

AfterReturningAdviceTest.java

import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
 
public class AfterReturningAdviceTest implements AfterReturningAdvice{  
    @Override  
    public void afterReturning(Object returnValue, Method method,  
            Object[] args, Object target)throws Throwable {  
        System.out.println("Additional concern " +
        		"after returning business logic.");  
    } 
}

applicationContext.java

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
   <bean id="businessLogic" 
   	class="com.javawithease.business.BusinessLogic"/>
   <bean id="afterReturningAdviceTest" 
   	class="com.javawithease.business.AfterReturningAdviceTest"/>
   <bean id="proxy" 
   	class="org.springframework.aop.framework.ProxyFactoryBean">
	<property name="target" ref="businessLogic"></property>  
	<property name="interceptorNames">  
	 <list>  
	  <value>afterReturningAdviceTest</value>  
	 </list>  
    </property> 
   </bean> 
</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("proxy", BusinessLogic.class);
 
	//Call implementBusinessLogic method of BusinessLogic bean.
	businessLogic.implementBusinessLogic();
 } 
}

Output:

Business logic executed.
Additional concern after returning business logic.

 
Download this example.

Spring AOP AspectJ Xml Configuration after-throwing Advice Example:

BusinessLogic.java

/**
 * This class will be used as a bean class.
 * @author javawithease
 */
public class BusinessLogic {
 public void implementBusinessLogic(int num){
   if(num == 0){  
        throw new ArithmeticException("Exception occures.");  
    }  
    else{  
        System.out.println("Business logic executed.");  
    }  
 }
}

AfterThrowingAdviceTest.java

import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;
 
public class AfterThrowingAdviceTest implements ThrowsAdvice{	
	public void afterThrowing(Method m,Object args[],
			Object target,Exception e) {
		System.out.println("Additional concern after" +
				" throwing exception in business logic.");
	}   
}

applicationContext.java

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
   <bean id="businessLogic" 
    class="com.javawithease.business.BusinessLogic"/>
   <bean id="afterThrowingAdviceTest" 
    class="com.javawithease.business.AfterThrowingAdviceTest"/>
   <bean id="proxy" 
    class="org.springframework.aop.framework.ProxyFactoryBean">  
	<property name="target" ref="businessLogic"></property>  
	<property name="interceptorNames">  
	 <list>  
	  <value>afterThrowingAdviceTest</value>  
	 </list>  
    </property> 
   </bean> 
</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("proxy", BusinessLogic.class);
 
  //Call implementBusinessLogic method of BusinessLogic bean.
  businessLogic.implementBusinessLogic(0);
 } 
}

Output:

Additional concern after throwing exception in business logic.
Exception in thread "main" java.lang.ArithmeticException: Exception occures.
	at com.javawithease.business.BusinessLogic.implementBusinessLogic(BusinessLogic.java:10)
	at com.javawithease.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.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:124)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625)
	at com.javawithease.business.BusinessLogic$$EnhancerByCGLIB$$b559ac2d.implementBusinessLogic(<generated>)
	at com.javawithease.business.Test.main(Test.java:17)

 
Download this example.

Spring AOP AspectJ Xml Configuration Around Advice Example:

BusinessLogic.java

/**
 * This class will be used as a bean class.
 * @author javawithease
 */
public class BusinessLogic {
	public void implementBusinessLogic(){
		System.out.println("Business logic executed.");
	}
}

AroundAdviceTest.java

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
 
public class AroundAdviceTest implements MethodInterceptor{  
 @Override
 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
  Object obj;  
  System.out.println("Additional concern before business logic.");
  obj=methodInvocation.proceed();  
  System.out.println("Additional concern after business logic.");
  return obj; 
 }	
}

applicationContext.java

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
   <bean id="businessLogic" 
    class="com.javawithease.business.BusinessLogic"/>
   <bean id="aroundAdviceTest" 
    class="com.javawithease.business.AroundAdviceTest"/>
   <bean id="proxy" 
    class="org.springframework.aop.framework.ProxyFactoryBean">  
	<property name="target" ref="businessLogic"></property>  
	<property name="interceptorNames">  
	 <list>  
	  <value>aroundAdviceTest</value>  
	 </list>  
    </property> 
   </bean> 
</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("proxy", BusinessLogic.class);
 
  //Call implementBusinessLogic method of BusinessLogic bean.
  businessLogic.implementBusinessLogic();
 } 
}

Output:

Additional concern before business logic.
Business logic executed.
Additional concern after business logic.

 
Download this example.