@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:

1
2
3
public interface Rating {
    int getRating();
}

3.2. Components Creation

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@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:

1
2
3
4
5
6
7
8
9
10
11
12
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:

Limiting Query Results with JPA and Spring Data JPA
SOAP Web service: Authentication trong JAX-WS
How to Iterate Over a Stream With Indices
Examine the internal DNS cache
Java Program to Implement Doubly Linked List
Servlet 3 Async Support with Spring MVC and Spring Security
Spring MVC Tutorial
Java Program to Implement Find all Cross Edges in a Graph
Wiring in Spring: @Autowired, @Resource and @Inject
Java Program to Implement Hash Tree
Getting Started with Stream Processing with Spring Cloud Data Flow
Java Program to Check whether Undirected Graph is Connected using DFS
Spring Boot Gradle Plugin
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Guava CharMatcher
Form Validation with AngularJS and Spring MVC
Read an Outlook MSG file
Netflix Archaius with Various Database Configurations
Encode a String to UTF-8 in Java
Refactoring Design Pattern với tính năng mới trong Java 8
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Merging Streams in Java
Spring @RequestParam Annotation
Java Program to Implement Cubic convergence 1/pi Algorithm
Java Program to Construct an Expression Tree for an Prefix Expression
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Hướng dẫn Java Design Pattern – Dependency Injection
Hashing a Password in Java
Using Custom Banners in Spring Boot
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Java Program to Implement RenderingHints API
Java Program to Implement AVL Tree