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 !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

3 thoughts on “Spring AOP – Aspects Ordering”

  1. What happens when two pieces of advice defined in the same aspect both need to run at the same join point.Can we still override the order with @Order annotation because I have tried it and it doesn’t work for such scenario.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.