Spring AOP – Aspects Ordering

In our last tutorial, we learned about spring aop key terms and example. These we created a logging aspect and then applied on UserManager class. Suppose you have multiple aspects in your application and they are can be applied on a certain method. When there’s more than one aspect applied to the same join point, the precedence/order of the aspects will not be determined unless you have explicitly specified it using either @Order annotation or org.springframework.core.Ordered interface. In this example, we will see an example of ordered aspects.

Specifying Aspects Ordering

As mentioned, to specify the order of aspects you have two ways:

1) Specifying aspects ordering using @Order annotation

This one is pretty simple. Use the annotation as below.

@Aspect
@Order(0)
public class EmployeeCRUDTransactionAspect
{
	@Before("execution(* EmployeeManager.getEmployeeById(..))")
	public void getEmployeeById(JoinPoint joinPoint) 
	{
		System.out.println("EmployeeCRUDTransactionAspect.getEmployeeById() : " + joinPoint.getSignature().getName());
	}
}

@Aspect
@Order(1)
public class EmployeeCRUDLoggingAspect 
{
	@Before("execution(* EmployeeManager.getEmployeeById(..))")
	public void logBefore(JoinPoint joinPoint) 
	{
		System.out.println("EmployeeCRUDAspect.logBefore() : " + joinPoint.getSignature().getName());
	}
}

2) Specifying aspects ordering by implementing org.springframework.core.Ordered interface

This one is too equally easy.

@Aspect
public class EmployeeCRUDLoggingAspect implements Ordered 
{
	//Override this method
	public int getOrder() {
		return 0;
	}
	
	@Before("execution(* EmployeeManager.getEmployeeById(..))")
	public void logBefore(JoinPoint joinPoint) 
	{
		System.out.println("EmployeeCRUDAspect.logBefore() : " + joinPoint.getSignature().getName());
	}
}

@Aspect
public class EmployeeCRUDTransactionAspect implements Ordered 
{
	//Override this method
	public int getOrder() {
		return 1;
	}

	@Before("execution(* EmployeeManager.getEmployeeById(..))")
	public void getEmployeeById(JoinPoint joinPoint) 
	{
		System.out.println("EmployeeCRUDTransactionAspect.getEmployeeById() : " + joinPoint.getSignature().getName());
	}
}

Now it’s time to test if ordering works. Configure both aspects in applicationContext.xml file.

<aop:aspectj-autoproxy />
<context:component-scan base-package="com.howtodoinjava.demo.aop" />

<bean id="transactionAspect" class="com.howtodoinjava.demo.aop.EmployeeCRUDTransactionAspect" />
<bean id="loggingAspect" class="com.howtodoinjava.demo.aop.EmployeeCRUDLoggingAspect" />

Let’s run the example as below:

public class TestAOP
{
	@SuppressWarnings("resource")
	public static void main(String[] args) 
	{

		ApplicationContext context = new ClassPathXmlApplicationContext("com/howtodoinjava/demo/aop/applicationContext.xml");
		EmployeeManager manager = context.getBean(EmployeeManager.class);

		manager.getEmployeeById(1);
	}
}

Output:

EmployeeCRUDAspect.logBefore() : getEmployeeById

EmployeeCRUDTransactionAspect.getEmployeeById() : getEmployeeById

Method getEmployeeById() called

Great. Spring AOP aspects ordering is working as expected.

Happy Learning !!

3 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Comments are closed for this article!

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.