Working with @Order Annotation in Spring: Ordering Beans and Dependencies

Alexander Obregon
3 min readAug 24, 2023
Image Source

Introduction

In the vast universe of the Spring framework, one annotation that often gets overlooked but holds great importance is @Order. In projects where you need to maintain a specific order of beans or when multiple beans of the same type exist, this annotation proves to be invaluable. This article will shed light on how @Order can be used to order beans and dependencies in Spring.

Understanding the @Order Annotation

Before diving into its usage, it’s crucial to understand what the @Order annotation is. In essence, the @Order annotation in the Spring framework helps to specify the order of beans or components. This is particularly useful when you have multiple beans of the same type and want to control their invocation or injection order.

Using @Order with @Bean

One of the primary uses of @Order is in combination with @Bean. Let’s look at a simple example:

@Configuration
public class AppConfig {

@Bean
@Order(2)
public String beanOne() {
return "Bean One";
}

@Bean
@Order(1)
public String beanTwo() {
return "Bean Two";
}

}

In the example above, we have two beans of type String. If you were to autowire a list of String beans in another component, beanTwo would appear before beanOne because of the order specified.

Ordering @Autowired Collections

If you have a scenario where multiple beans of the same type exist, Spring allows you to autowire them as a collection. The @Order annotation determines the order in which they are injected.

Example:

@Service
public class ServiceRunner {

@Autowired
private List<String> beans;

public void printBeans() {
beans.forEach(System.out::println);
}
}

When you invoke the printBeans() method, it would print:

Bean Two
Bean One

Using with @Component and Derivatives

The @Order annotation can also be used with @Component, @Service, @Repository, and @Controller annotations. This is especially helpful when working with beans defined as components instead of configuration-based beans.

Ordering Aspect Execution

If you’re familiar with Aspect-Oriented Programming (AOP) in Spring, you’ll appreciate the need to order aspects. @Order is incredibly handy here.

Suppose you have two aspects, LoggingAspect and SecurityAspect, and you wish to ensure that logging occurs after security checks. Here's how you'd use the @Order annotation:

@Aspect
@Order(2)
@Component
public class LoggingAspect {
// Logging logic here
}

@Aspect
@Order(1)
@Component
public class SecurityAspect {
// Security logic here
}

Ordering Filters

Another common use case of @Order is while defining filters, especially when you need specific filters to run before others:

@Component
@Order(1)
public class TransactionFilter implements Filter {
// Filter logic here
}

@Component
@Order(2)
public class AuthenticationFilter implements Filter {
// Filter logic here
}

In the above case, the TransactionFilter would execute before the AuthenticationFilter.

Importance of Order Value

While using @Order, it's good to leave gaps between the order values. This practice makes it easier to add new components in between without reordering everything. For instance, using order values like 10, 20, 30 instead of 1, 2, 3 can prove beneficial in the long run.

Conclusion

The @Order annotation is a nifty tool in the Spring framework, offering granular control over the order of beans and components. Whether it's ordering beans, aspects, or filters, @Order ensures your components work in harmony and sequence. As your project grows, you'll find more scenarios where maintaining a specific order becomes paramount. It's here that the @Order annotation will prove its mettle, ensuring your Spring beans and dependencies work seamlessly together.

  1. Spring Framework Documentation on @Order
  2. Spring Boot Documentation on Auto-Configuration
  3. Spring AOP Documentation
Spring Boot icon by Icons8

--

--

Alexander Obregon

Software Engineer, fervent coder & writer. Devoted to learning & assisting others. Connect on LinkedIn: https://www.linkedin.com/in/alexander-obregon-97849b229/