English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Spring AOP AspectJ XML Configuration Example

Spring allows you to define aspects, advice, and pointcuts in XML files.

On the previous page, we have seen an AOP example using annotations. Now, we will see the same example through an XML configuration file.

Let's see the XML elements used to define the advice.

aop: before Apply it before calling the actual business logic method. aop: after Apply it after calling the actual business logic method. aop: after returningApply it after calling the actual business logic method. It can be used to intercept the return value in the advice. aop: around Apply it before and after calling the actual business logic method. aop: after throwingApply it if the actual business logic method throws an exception.
Note: To understand the concept of AOP and its advantages, please visit here. AOP Concept Tutorial

1aop: before

Apply the "before aspectJ advice" before the actual business logic method. You can perform any action here, such as transformation, authentication, etc.

Create a class that contains the actual business logic.

File: Operation.java
package com.w;3codebox;
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, let's create an aspect class that is included before the advice.

File: TrackOperation.java

package com.w;3codebox;
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 the bean.

File: 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.w3codebox.Operation">	</bean>
<bean id="trackAspect" class="com.w3codebox.TrackOperation"></bean>
<aop:config>
  <aop:aspect id="myaspect" ref="trackAspect" >
     <!-- @Before -->
     <aop:pointcut id="pointCutBefore"	expression="execution(* com.w3codebox.Operation.*(..))" />
     <aop:before method="myadvice" pointcut-ref="pointCutBefore" />
  </aop:aspect>
</aop:config>
</beans>

Now, let's call the actual method.

File: Test.java

package com.w;3codebox;
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, before calling the msg(), m(), and k() methods, other issues are also printed.


2Example after aop:

After calling the actual business logic method, AspectJ after advice is applied. It can be used for maintaining logs, security, notifications, etc.

Here, we assume Operation.java , TrackOperation.java and}} Test.java The file is the same as the example in aop:

Now create the applicationContext.xml file that defines the bean.

File: 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.w3codebox.Operation">	</bean>
<bean id="trackAspect" class="com.w3codebox.TrackOperation"></bean>
<aop:config>
  <aop:aspect id="myaspect" ref="trackAspect" >
     <!-- @After -->
     <aop:pointcut id="pointCutAfter"	expression="execution(* com.w3codebox.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 after calling the msg(), m(), and k() methods, other issues also appear.


3Example of after returning AOP

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

Create

File: Operation.java

package com.w;3codebox;
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 included after the return advice.

File: TrackOperation.java

package com.w;3codebox;
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...");
	}
}

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

File: Test.java

Now create the Test class that calls the actual method.

package com.w;3codebox;
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.w3codebox.Operation.m()
Result in advice: 2
...end of after returning advice...
2
calling k...
k() method invoked
additional concern
Method Signature: int com.w3codebox.Operation.k()
Result in advice: 3
...end of after returning advice...
3

You can see that the return value is printed twice, once by the TrackOperation class and the second time by the Test class.


4, aop: around

AspectJ around advice is applied before and after the actual business logic method is called.

Create a class

File: Operation.java

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

Create a class that wraps around the advice.

You need to pass in the PreceedingJoinPoint In order to call the request() method, we need to reference,

File: TrackOperation.java

package com.w;3codebox;
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;
	}
}

File: 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.w3codebox.Operation">	</bean>
<bean id="trackAspect" class="com.w3codebox.TrackOperation"></bean>
<aop:config>
  <aop:aspect id="myaspect" ref="trackAspect" >
     <!-- @Around -->
     <aop:pointcut id="pointCutAround"	expression="execution(* com.w3codebox.Operation.*(..))" />
     <aop:around method="myadvice" pointcut-ref="pointCutAround" />
  </aop:aspect>
</aop:config>
</beans>

File: Test.java

Now create the Test class that calls the actual method.

package com.w;3codebox;
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 other issues are also printed before and after calling msg() and displaying the method.


5aop: after-throwing

By using after advice, we can print exceptions in the TrackOperation class. Let's look at an example of AspectJ AfterThrowing advice.

Create a class containing business logic.

File: Operation.java

package com.w;3codebox;
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 contained after the advice is thrown.

Here, we also need to pass a Throwable reference so that we can intercept exceptions here.

File: TrackOperation.java

package com.w;3codebox;
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...");
	}
}

File: 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.w3codebox.Operation">	</bean>
<bean id="trackAspect" class="com.w3codebox.TrackOperation"></bean>
<aop:config>
  <aop:aspect id="myaspect" ref="trackAspect" >
     <!-- @AfterThrowing -->
     <aop:pointcut id="pointCutAfterThrowing"	expression="execution(* com.w3codebox.Operation.*(..))" />
     <aop:after-throwing method="myadvice" throwing="error" pointcut-ref="pointCutAfterThrowing" />
  </aop:aspect>
</aop:config>
</beans>

File: Test.java

Now create the Test class that calls the actual method.

package com.w;3codebox;
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 voting
calling validate again...
additional concern
Method Signature: void com.w3codebox.Operation.validate(int)
Exception is: java.lang.ArithmeticException: Not valid age
end of after throwing advice...
java.lang.ArithmeticException: Not valid age