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.

Spring AOP AspectJ Xml Configuration Before Advice Example:

BusinessLogic.java

/**
 * This class will be used as a bean class.
 * @author W3schools360
 */
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.xml




   
   
   
	  
	  
	   
	  beforeAdviceTest  
	   
     
    

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 W3schools360
 */
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.xml




   
   
   
	  
	  
	   
	  afterReturningAdviceTest  
	   
     
    

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 W3schools360
 */
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.xml




   
   
     
	  
	  
	   
	  afterThrowingAdviceTest  
	   
     
    

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.w3schools360.business.BusinessLogic.implementBusinessLogic(BusinessLogic.java:10)
	at com.w3schools360.business.BusinessLogic$$FastClassByCGLIB$$19ab96db.invoke()
	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.w3schools360.business.BusinessLogic$$EnhancerByCGLIB$$b559ac2d.implementBusinessLogic()
	at com.w3schools360.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 W3schools360
 */
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.xml




   
   
     
	  
	  
	   
	  aroundAdviceTest  
	   
     
    

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.