Introduction to @Order annotation in Spring

Quick guide on How to set a custom order to Spring Beans using Spring @Order annotation.

Overview

The @Order annotation in Spring defines a custom sort order for Spring Beans. When there are more than one auto wired components, Spring maintains the order in which those components are scanned.

For example, consider we are applying two aspects on same method. By default, spring will keep those two Aspects in the same order in which they were discovered by the component scan. If you want them to run in a particular order, you can use @Order annotation by passing an integer to it.

In this tutorial we will first have an overview of @Order annotation. Then, we will look at examples of ordering Filters, Aspects, and Spring Beans.

Introduction to Spring @Order annotation

The @Order annotation defines a sort order for an annotated Spring component. It has an optional value field of type integer and it indicates the preferred precedence of the component. If the value field is omitted, the component is set to lowest possible precedence.

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Documented
public @interface Order {
    int value() default 2147483647;
}Code language: Java (java)

Initially, Spring allowed using @Order annotation only the Aspects of AOP. However, later (after Spring 4.0), it opened for various other use cases. That includes collection injections, @Bean factory methods, and filters etc.

To set a lowest precedence value we can use Ordered.LOWEST_PRECEDENCE, which is also the default value. Similarly, we can use Ordered.HIGHEST_PRECEDENCE to define highest possible precedence for a component.

Set Sort Order for Filters

When we have a multiple Request and Response Filters in a Spring or Spring Boot application, we can use @Order annotation to define the order in which the filters should be applied.

@Order(1)
@Component
public class FirstFilter implements Filter { .. }



@Order(2)
@Component
public class SecondFilter implements Filter {...}



@Order(2)
@Component
public class ThirdFilter implements Filter {...}Code language: Java (java)

In the above snippet, there are three filters and each of them is marked with @Order.

If, there are multiple components having the same order value, Spring executes them in their default order of appearance. Thus, the SecondFilter and ThirdFilter will run by Spring’s default order.

Ordering Aspects in AOP

Similarly, when there are more than one aspects available, Spring considers the precedence order if it is provided.

First Aspect

@Aspect
@Order(2)
@Component
public class FirstAspect {
    @Before("execution(* com.amitph.spring.products.Products.getProduct(..))")
    public void beforeGetProduce(JoinPoint joinPoint) {
        System.out.println("Before getting product #1");
    }
}Code language: Java (java)

Second Aspect

@Aspect
@Order(1)
@Component
public class SecondAspect {
    @Before("execution(* com.amitph.spring.products.Products.getProduct(..))")
    public void beforeGetProduce(JoinPoint joinPoint) {
        System.out.println("Before getting product #2");
    }
}Code language: Java (java)

As the SecondAspect has the higher precedence, when the getProduct() method is executed, spring will apply it before the FirstAspect.

Ordering @Bean Factory Methods

We can also order Injecting Collections in Spring by putting @Order annotation on the @Bean methods.

Consider, there are three @Bean methods and each of them returns a String.

@Bean
@Order(2)
public String getString1() {
    return "one";
}

@Bean
@Order(3)
String getString2() {
    return "two";
}

@Bean
@Order(1)
String getString3() {
    return "three";
}Code language: Java (java)

When we auto wire them into a List

private final List<String> stringList;

@Autowired
public FileProcessor(List<String> stringList) {
    this.stringList = stringList;
}Code language: Java (java)

Spring sorts the beans in the specified order. Thus, printing the list we get:

three
one
two

Similarly, when we are injecting a multiple beans of same type as a collection we can set a custom sort order on the individual bean classes. However, we have already talked about it in detail here.

Summary

This tutorial talked about Spring @Order annotation. This annotation is used to set a custom sort order for Spring Beans, @Bean factory methods, and Aspects etc. The component with highest order value takes the precedence.
For more on Spring & Spring Boo, please visit Spring Tutorials.