HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Spring AOP – Aspects Ordering

By Lokesh Gupta | Filed Under: Spring AOP

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

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

3
Leave a Reply

This comment form is under antispam protection
3 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
Vinay

What happen if order has same for two aspect?

Vote Up0Vote Down  Reply
4 months ago
Hasnat

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.

Vote Up0Vote Down  Reply
4 years ago
Binh Thanh Nguyen

Thanks, nice post series.

Vote Up0Vote Down  Reply
4 years ago

Search Tutorials

Spring AOP Tutorial

  • AOP – Introduction
  • AOP – Annotation Config
  • AOP – XML Config
  • AOP – @Before
  • AOP – @After
  • AOP – @Around
  • AOP – @AfterReturning
  • AOP – @AfterThrowing
  • AOP – Before Advice
  • AOP – After Advice
  • AOP – Around Advice
  • AOP – After-Returning Advice
  • AOP – After-Throwing Advice
  • AOP – Pointcut Expressions
  • AOP – Aspects Ordering
  • AOP – Transactions
  • AOP – Interview Questions

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz