@Order in Spring

1. Overview

In this tutorial, we’re going to learn about Spring’s @Order annotation. The @Order annotation defines the sorting order of an annotated component or bean.

It has an optional value argument which determines the order of the component; the default value is Ordered.LOWEST_PRECEDENCE. This marks that the component has the lowest priority among all other ordered components.

Similarly, the value Ordered.HIGHEST_PRECEDENCE can be used for overriding the highest priority among components.

2. When to Use @Order

Before Spring 4.0, the @Order annotation was used only for the AspectJ execution order. It means the highest order advice will run first.

Since Spring 4.0, it supports the ordering of injected components to a collection. As a result, Spring will inject the auto-wired beans of the same type based on their order value.

Let’s explore it with a quick example.

3. How to Use @Order

First of all, let’s set up our project with the relevant interface and classes.

3.1. Interface Creation

Let’s create the Rating interface that determines the rating of a product:

public interface Rating {
    int getRating();
}

3.2. Components Creation

Finally, let’s create three components that define the ratings of some products:

@Component
@Order(1)
public class Excellent implements Rating {

    @Override
    public int getRating() {
        return 1;
    }
}

@Component
@Order(2)
public class Good implements Rating {

    @Override
    public int getRating() {
        return 2;
    }
}

@Component
@Order(Ordered.LOWEST_PRECEDENCE)
public class Average implements Rating {

    @Override
    public int getRating() {
        return 3;
    }
}

Note that the Average class has the lowest priority because of its overridden value.

4. Testing Our Example

Up until now, we’ve created all the required components and the interface to test the @Order annotation. Now, let’s test it to confirm that it works as expected:

public class RatingRetrieverUnitTest { 
    
    @Autowired
    private List<Rating> ratings;
    
    @Test
    public void givenOrder_whenInjected_thenByOrderValue() {
        assertThat(ratings.get(0).getRating(), is(equalTo(1)));
        assertThat(ratings.get(1).getRating(), is(equalTo(2)));
        assertThat(ratings.get(2).getRating(), is(equalTo(3)));
    }
}

5. Conclusion

We’ve learned about the @Order annotation in this quick article. We can find the application of @Order in various use cases – where the ordering of the auto-wired components matter. One example is the Spring’s request filters.

Due to its influence on injection precedence, it may seem like it might influence the singleton startup order also. But in contrast, the dependency relationships and @DependsOn declarations determine the singleton startup order.

All examples mentioned in this tutorial can be found over on Github.

Related posts:

Introduction to Liquibase Rollback
A Guide to Java SynchronousQueue
Java Program to Solve Tower of Hanoi Problem using Stacks
Java Program to Implement Horner Algorithm
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Hướng dẫn Java Design Pattern – Strategy
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
How to Return 404 with Spring WebFlux
Quick Guide to @RestClientTest in Spring Boot
Java Program to Implement AttributeList API
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Lấy ngày giờ hiện tại trong Java
Java Program to Implement Regular Falsi Algorithm
Java Program to Perform the Unique Factorization of a Given Number
Java Program to Check Multiplicability of Two Matrices
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Spring Autowiring of Generic Types
Java Program to Find Transitive Closure of a Graph
So sánh HashMap và HashSet trong Java
Java Program to implement Bi Directional Map
Guide to the Volatile Keyword in Java
Call Methods at Runtime Using Java Reflection
Java Stream Filter with Lambda Expression
Java Program to Implement Fibonacci Heap
Java Program to Implement Stack using Two Queues
Java Program to Check whether Graph is Biconnected
Spring Boot - CORS Support
Java Program to Implement Sieve Of Eratosthenes
Send an email with an attachment
Giới thiệu Aspect Oriented Programming (AOP)
Guava Collections Cookbook
Hướng dẫn Java Design Pattern – Object Pool