HomeSpring Framework@Order in Spring

@Order in Spring

In this guide we will see examples on how to use @Order annotation in Spring. @Order annotation can be used on components or bean methods to define sort order in collection items or in the array or list to be sorted in a specific order. 

@Order annotation has value attribute, it is used to define order of component or bean, and which is optional. The default value is Ordered.LOWEST_PRECEDENCE, indicating lowest priority. Similarly, Ordered.HIGHEST_PRECEDENCE indicates highest priority among components.

1. @Order on Components

Let’s see an example on how to use order the components. Let’s create interface Fruit and Apple, Banana, Mango and Orange as implementation classes of Fruit interface and then make them components and give the priority using @Order.

public interface Fruit {
  
}
@Component
@Order(3)
public class Apple implements Fruit {

  @Override
  public String toString() {
    return "Apple";
  }
}

@Component
@Order(4)
public class Banana implements Fruit {

  @Override
  public String toString() {
    return "Banana";
  }
}

@Component
@Order(2)
public class Mango implements Fruit {

  @Override
  public String toString() {
    return "Mango";
  }
}

@Component
@Order(1)
public class Orange implements Fruit {

  @Override
  public String toString() {
    return "Orange";
  }
}

Let’s inject components to collection.

public class CollectionTestBean {

	@Autowired
	private List<Fruit> fruitList;

	public void printFruitList() {
		System.out.println("printFruitList:"+fruitList);
	}

	// ...

}

Testing configuration.

public class ApplicationCollectionDemo {
	
	public static void main(String[] args) {
		
		ApplicationContext ctxt = new AnnotationConfigApplicationContext(CollectionBeanConfig.class, FruitConfig.class);
		CollectionTestBean collectionTestBean = ctxt.getBean(CollectionTestBean.class);
		
		collectionTestBean.printFruitList();

	}
}

Results when invoke printFruitList():

printFruitList:[Orange, Mango, Apple, Banana]

2. @Order on Beans

Let’s have a look into another example on how to order specific beans.

@Configuration
public class CollectionBeanConfig {
  
  // ...
  
  @Bean
  public Animal getTiger() {
    return new Animal("Tiger");
  }
  
  @Bean
  @Order(1)
  public Animal getDeer() {
    return new Animal("Deer");
  }
  
  @Bean
  public Animal getKangaroo() {
    return new Animal("Kangaroo");
  }
  
  @Bean
  @Order(2)
  public Animal getFox() {
    return new Animal("Fox");
  }
}
public class CollectionTestBean {
  @Autowired
  private List<Animal> animalList;

  public void printAnimalList() {
    System.out.println("printAnimalList:"+animalList);
  }
  // ...
}

Output results on invoking printAnimalList() to verify Order:

printAnimalList:[Deer, Fox, Tiger, Kangaroo]

3. Conclusion

@Order annotation is used to define sorting order of component or bean. In this tutorial we have seen several examples on how to use @Order annotation using Spring Framework.

You can get source code from our GitHub repository.

4. References

  1. Spring Reference Document
  2. Spring Java Document

Other Related Tutorials:

  1. Spring @Autowired
  2. Spring @Lazy
  3. Spring Boot XML Configuration
  4. Spring Injecting Collection
  5. Spring @Primary
  6. Spring @Import
  7. Spring Component Scanning
  8. Spring @ComponentScan Filter Types

LEAVE A REPLY

Please enter your comment!
Please enter your name here