SoFunction
Updated on 2025-04-06

How to use the interface and annotation of Spring sorting mechanism

1. Requirements scenarios for Spring sorting

In actual development, you may encounter multiple beans or configuration classes that need to be executed in order when initializing in containers.

  • Need to make sure that one bean is loaded before another bean is initialized
  • Multiple@ConfigurationBeans in the configuration class must be registered in a specific order
  • When Spring container starts, make sure some functions execute before others

Spring provides a variety of mechanisms to meet these needs, the most common of which is throughOrderedinterface,PriorityOrderedinterface,@OrderAnnotations and@PriorityAnnotations to control the ordering priority of the bean.

2. Sorting mechanism in Spring

1. Ordered interface

  OrderedThe interface is the most basic sorting interface in Spring, which is used to define the sorting priority of a bean. When a bean is implementedOrderedWhen interfaced, the Spring container will be based on the bean'sgetOrder()The method returns the value to sort. The smaller the return value, the higher the priority.

Example

import ;
public class MyBean implements Ordered {
    @Override
    public int getOrder() {
        return 1; // The smaller the higher the priority    }
}

In the above example,MyBeanImplementedOrderedThe interface and returned1, which means it has high priority.

2. PriorityOrdered interface

  PriorityOrderedyesOrderedAn extension of the interface to define higher priority. accomplishPriorityOrderedThe interface's bean will be considered to have a higher priority, even if itgetOrder()The returned value is larger and will be initialized by the container first.

Example

import ;
public class MyPriorityBean implements PriorityOrdered {
    @Override
    public int getOrder() {
        return 10; // Even if a larger value is returned, its priority will be higher    }
}

  PriorityOrderedThe interface is designed to compareOrderedUsed at higher priority. For example, when some beans need to be initialized before all normal beans are loaded, usePriorityOrderedIt can be ensured to load first.

3. @Order annotation

  @OrderThe annotation isOrderedAn annotated version of the interface, which can be used directly on a class or method, to specify the loading priority of a bean. This annotation isOrderedA simple form of interface, suitable for configuring classes, beans, AOP facets, etc. in Spring containers.

Example

import ;
import ;
import ;
@Configuration
public class Config {
    @Bean
    @Order(1)
    public MyBean myBean() {
        return new MyBean();
    }
}

In the above code,@Order(1)Annotation specifiedmyBeanThe priority is 1, meaning it will be loaded first.

4. @Priority annotation

  @PriorityAnnotations and@OrderAnnotation is similar, but it is used to specify higher priority, equivalent to implementationPriorityOrderedinterface. This annotation is usually used to indicate that the bean should have a ratio@OrderAnnotate higher priority.

Example

import ;
@Priority(1)
public class MyPriorityBean {
    // Bean implementation}

In this example,@Priority(1)Annotation representationMyPriorityBeanThe priority is 1, ensuring that it is loaded before other normal beans.

3. How to choose the appropriate sorting mechanism?

  • useOrderedInterface: If you want to control the sorting of a bean, you can make it implement the Ordered interface and return an integer value to indicate priority.The smaller the value, the higher the priority
  • usePriorityOrderedInterface: When you need a higher priority bean, you can use the PriorityOrdered interface. These beans will be consideredMore preferred than beans that implement Ordered interfaces
  • use@OrderNote: If you don't want to implement it manuallyOrdered interface, @Order annotation is a good oneAlternatives, especially when you configure the @Configuration class or use Bean annotation

use@PriorityAnnotation: When you need to define a higher priority bean, you can use the @Priority annotation, whichEquivalent to implementing the PriorityOrdered interface

4. Summary

Spring provides a variety of mechanisms to controlBean loading order and priorityThe main sorting mechanisms includeOrderedinterface,PriorityOrderedinterface,@OrderAnnotations and@Priorityannotation. By using these mechanisms reasonably, the loading order of beans in Spring containers can be effectively managed, ensuring that different functional modules are initialized in the expected order.

This is the article about Spring sorting mechanism: the use of interfaces and annotations. For more related content on Spring interfaces and annotations, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!