Spring AOP AspectJ Xml Configuration Example

Spring empowers you to characterize the angles, advices and pointcuts in xml record. In the past page, we have seen the aop samples utilizing annotations. Presently we are going to see same cases by the xml setup document.

Let's see the xml elements that are used to define advice.

  1. aop:before It is applied before calling the actual business logic method.
  2. aop:after It is applied after calling the actual business logic method.
  3. aop:after-returning it is applied after calling the actual business logic method. It can be used to intercept the return value in advice.
  4. aop:around It is applied before and after calling the actual business logic method.
  5. aop:after-throwing It is applied if actual business logic method throws exception.

1- aop: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.

Operation.java
package com.javatportal;
public  class Operation{
	public void msg(){System.out.println("msg method invoked");}
	public int m(){System.out.println("m method invoked");return 2;}
	public int k(){System.out.println("k method invoked");return 3;}
}

Now, create the aspect class that contains before advice.

TrackOperation.java
package com.javatportal;
import org.aspectj.lang.JoinPoint;
public class TrackOperation{
	public void myadvice(JoinPoint jp)//it is advice
	{
		System.out.println("additional concern");
		//System.out.println("Method Signature: "  + jp.getSignature());
	}
}

Now create the applicationContext.xml file that defines beans.

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:aspectj-autoproxy />

<bean id="opBean" class="com.javatportal.Operation">	</bean>
<bean id="trackAspect" class="com.javatportal.TrackOperation"></bean>
		
<aop:config>
  <aop:aspect id="myaspect" ref="trackAspect" >
     <!-- @Before -->
     <aop:pointcut id="pointCutBefore"	expression="execution(* com.javatportal.Operation.*(..))" />
     <aop:before method="myadvice" pointcut-ref="pointCutBefore" />
  </aop:aspect>
</aop:config>
	
</beans>

Now, let's call the actual method.

Test.java
package com.javatportal;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test{
	public static void main(String[] args){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Operation e = (Operation) context.getBean("opBean");
		System.out.println("calling msg...");
		e.msg();
		System.out.println("calling m...");
		e.m();
		System.out.println("calling k...");
		e.k();
	}
}

Output

calling msg...
additional concern
msg() method invoked
calling m...
additional concern
m() method invoked
calling k...
additional concern
k() method invoked

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


2- aop: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, TrackOperation.java and Test.java files are same as given in aop:before example.

Now create the applicationContext.xml file that defines beans.

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:aspectj-autoproxy />

<bean id="opBean" class="com.javatportal.Operation">	</bean>
<bean id="trackAspect" class="com.javatportal.TrackOperation"></bean>
		
<aop:config>
  <aop:aspect id="myaspect" ref="trackAspect" >
     <!-- @After -->
     <aop:pointcut id="pointCutAfter"	expression="execution(* com.javatportal.Operation.*(..))" />
     <aop:after method="myadvice" pointcut-ref="pointCutAfter" />
  </aop:aspect>
</aop:config>
	
</beans>

Output

calling msg...
msg() method invoked
additional concern
calling m...
m() method invoked
additional concern
calling k...
k() method invoked
additional concern

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


3- aop:after-returning example

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

Create the class that contains business logic.

Operation.java
package com.javatportal;
public  class Operation{
	public int m(){System.out.println("m() method invoked");return 2;}
	public int k(){System.out.println("k() method invoked");return 3;}
}

Create the aspect class that contains after returning advice.

TrackOperation.java
package com.javatportal;

import org.aspectj.lang.JoinPoint;

public class TrackOperation{
	public void myadvice(JoinPoint jp,Object result)//it is advice (after advice)
	{
		System.out.println("additional concern");
		System.out.println("Method Signature: "  + jp.getSignature());
		System.out.println("Result in advice: "+result);
		System.out.println("end of after returning advice...");
	}
}
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:aspectj-autoproxy />

<bean id="opBean" class="com.javatportal.Operation">	</bean>
	
<bean id="trackAspect" class="com.javatportal.TrackOperation"></bean>
		
<aop:config>
  <aop:aspect id="myaspect" ref="trackAspect" >
     <!-- @AfterReturning -->
     <aop:pointcut id="pointCutAfterReturning"	expression="execution(* com.javatportal.Operation.*(..))" />
     <aop:after-returning method="myadvice" returning="result" pointcut-ref="pointCutAfterReturning" />
  </aop:aspect>
</aop:config>
	
</beans>

Test.java

Now create the Test class that calls the actual methods.

package com.javatportal;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test{
	public static void main(String[] args){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Operation e = (Operation) context.getBean("opBean");
		System.out.println("calling m...");
		System.out.println(e.m());
		System.out.println("calling k...");
		System.out.println(e.k());
	}
}

Output

calling m...
m() method invoked
additional concern
Method Signature: int com.javatportal.Operation.m()
Result in advice: 2
end of after returning advice...
2
calling k...
k() method invoked
additional concern
Method Signature: int com.javatportal.Operation.k()
Result in advice: 3
end of after returning advice...
3

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


4- aop:around example

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

Create a class that contains actual business logic.

Operation.java
package com.javatportal;
public  class Operation{
	public void msg(){System.out.println("msg() is invoked");}
	public void display(){System.out.println("display() is invoked");}
}

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.

TrackOperation.java
package com.javatportal;
import org.aspectj.lang.ProceedingJoinPoint;
public class TrackOperation
{
	public Object myadvice(ProceedingJoinPoint pjp) throws Throwable 
	{
		System.out.println("Additional Concern Before calling actual method");
		Object obj=pjp.proceed();
		System.out.println("Additional Concern After calling actual method");
		return obj;
	}
}
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:aspectj-autoproxy />

<bean id="opBean" class="com.javatportal.Operation">	</bean>
	
<bean id="trackAspect" class="com.javatportal.TrackOperation"></bean>
		
<aop:config>
  <aop:aspect id="myaspect" ref="trackAspect" >
     <!-- @Around -->
     <aop:pointcut id="pointCutAround"	expression="execution(* com.javatportal.Operation.*(..))" />
     <aop:around method="myadvice" pointcut-ref="pointCutAround" />
  </aop:aspect>
</aop:config>
	
</beans>
Test.java

Now create the Test class that calls the actual methods.

package com.javatportal;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test{
	public static void main(String[] args){
		ApplicationContext context = new classPathXmlApplicationContext("applicationContext.xml");
		
		Operation op = (Operation) context.getBean("opBean");
		op.msg();
		op.display();
	}
}

Output

Additional Concern Before calling actual method
msg() is invoked
Additional Concern After calling actual method
Additional Concern Before calling actual method
display() is invoked
Additional Concern After calling actual method

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


5) aop:after-throwing 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.

Operation.java
package com.javatportal;
public  class Operation{
	public void validate(int age)throws Exception{
	if(age <18){
		throw new ArithmeticException("Not valid age");
	}
	else{
		System.out.println("Thanks for vote");
	}
	}
	
}

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.

TrackOperation.java
package com.javatportal;
import org.aspectj.lang.JoinPoint;
public class TrackOperation{
			      
	public void myadvice(JoinPoint jp,Throwable error)//it is advice
	{
		System.out.println("additional concern");
		System.out.println("Method Signature: "  + jp.getSignature());
		System.out.println("Exception is: "+error);
		System.out.println("end of after throwing advice...");
	}
}
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:aspectj-autoproxy />
<bean id="opBean" class="com.javatportal.Operation">	</bean>
<bean id="trackAspect" class="com.javatportal.TrackOperation"></bean>
		
<aop:config>
  <aop:aspect id="myaspect" ref="trackAspect" >
     <!-- @AfterThrowing -->
     <aop:pointcut id="pointCutAfterThrowing"	expression="execution(* com.javatportal.Operation.*(..))" />
     <aop:after-throwing method="myadvice" throwing="error" pointcut-ref="pointCutAfterThrowing" />
  </aop:aspect>
</aop:config>
	
</beans>
Test.java

Now create the Test class that calls the actual methods.

package com.javatportal;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test{
	public static void main(String[] args){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Operation op = (Operation) context.getBean("opBean");
		System.out.println("calling validate...");
		try{
			op.validate(19);
		}catch(Exception e){System.out.println(e);}
		System.out.println("calling validate again...");
		
		try{
		    op.validate(11);
		}catch(Exception e){System.out.println(e);}
	}
}

Output

calling validate...
Thanks for vote
calling validate again...
additional concern
Method Signature: void com.javatportal.Operation.validate(int)
Exception is: java.lang.ArithmeticException: Not valid age
end of after throwing advice...
java.lang.ArithmeticException: Not valid age