In this post, we will discuss the @Order annotation. We are covering various features of this annotation.
Introduction
<em>@Order annotation</em>
defines the sort order for an annotated component. This annotation is available from Spring 2.0. This annotation has an optional value argument which represents an order value as defined in the Ordered
interface. The default value is Ordered.LOWEST_PRECEDENCE
, showing lowest priority, similarly, the value Ordered.HIGHEST_PRECEDENCE
for overriding the highest priority.
1. @Order annotation
Before Spring 4, @Order
annotation was only used for ordering AspectJ
aspects. Post Spring 4, Spring @Order annotation support ordering of auto-wired components in collections like Lists and Arrays.Let’s try to understand few important points for the @Order
annotation in Spring
- The
@Order annotation
in Spring framework is for the execution precedence. - The highest precedence advice runs first.
- The lower number have the highest precedence (order annotation with 1 will run before 2)
2. How to Use @Order
To understand this annotation, let’s build the following example.
- We want to recommend a car to the potential customers.
- Toyota is the first recommendation.
- Honda is the second recommendation.
- UsedCard will be the lowest priority recommendation.
2.1. Car Interface
Let’s create our Car interface.
public interface Car {
/**
* This method is responsible for recommending car to customer based on our algorithm
* @return Recommended car
*/
String getCarRecommendation();
}
2.2. Car Creations
let’s create three different instances of the car and set the order based on our algorithm.
@Component
@Order(1)
public class Toyota implements Car {
@Override
public String getCarRecommendation() {
return "Toyota";
}
}
@Component
@Order(2)
public class Honda implements Car {
@Override
public String getCarRecommendation() {
return "Honda";
}
}
@Component
@Order(Ordered.LOWEST_PRECEDENCE)
public class UsedCar implements Car {
@Override
public String getCarRecommendation() {
return "Certified Car";
}
}
In the “UsedCar
” section, we added the lowest priority.
3. Test Application
Let’s test our application to see how this annotation work.
@RunWith(SpringRunner.class)
@SpringBootTest
public class OrderAnnotationTest {
@Autowired
private List < Car > cars;
@Test
public void test_spring_order_annotation() {
assertThat(cars.get(0).getCarRecommendation(), is(equalTo("Toyota")));
assertThat(cars.get(1).getCarRecommendation(), is(equalTo("Honda")));
assertThat(cars.get(2).getCarRecommendation(), is(equalTo("Certified Car")));
}
}
If you see the above example, Toyota is the top of list as we have given the highest priority using the order annotation.
4. Beans and Components
We can use the Spring order annotation on both at Components level or while creating the beans. In the above example we saw how to use the @Order annotation on the class level. Let’s see how to use this annotation to order specific beans.
@Configuration
public class CarConfiguration {
@Bean
@Order(1)
public Car getToyota() {
return new Toyota();
}
@Bean
@Order(2)
public Car getHonda() {
return new Honda();
}
@Bean
@Order(3)
public Car getUsedCar() {
return new UsedCar();
}
}
Summary
In this article, we discuss the Spring @Order annotation. We learned when to use this annotation and how to customize its behavior. We can find source code for this post over Github.
How did this object get populated with car objects? Its not defined anywhere.
@Autowired
private List cars;
Hello Amit,
Please refer to the source code for complete end to end working example.I am putting the value in the implementation class.
https://github.com/umeshawasthi/javadevjournal/tree/master/spring/spring-order-annotation
Thank you Umesh