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
@Configuration
Beans 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 throughOrdered
interface,PriorityOrdered
interface,@Order
Annotations and@Priority
Annotations to control the ordering priority of the bean.
2. Sorting mechanism in Spring
1. Ordered interface
Ordered
The interface is the most basic sorting interface in Spring, which is used to define the sorting priority of a bean. When a bean is implementedOrdered
When 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,MyBean
ImplementedOrdered
The interface and returned1
, which means it has high priority.
2. PriorityOrdered interface
PriorityOrdered
yesOrdered
An extension of the interface to define higher priority. accomplishPriorityOrdered
The 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 } }
PriorityOrdered
The interface is designed to compareOrdered
Used at higher priority. For example, when some beans need to be initialized before all normal beans are loaded, usePriorityOrdered
It can be ensured to load first.
3. @Order annotation
@Order
The annotation isOrdered
An 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 isOrdered
A 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 specifiedmyBean
The priority is 1, meaning it will be loaded first.
4. @Priority annotation
@Priority
Annotations and@Order
Annotation is similar, but it is used to specify higher priority, equivalent to implementationPriorityOrdered
interface. This annotation is usually used to indicate that the bean should have a ratio@Order
Annotate higher priority.
Example
import ; @Priority(1) public class MyPriorityBean { // Bean implementation}
In this example,@Priority(1)
Annotation representationMyPriorityBean
The priority is 1, ensuring that it is loaded before other normal beans.
3. How to choose the appropriate sorting mechanism?
- use
Ordered
Interface: 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
- use
PriorityOrdered
Interface: 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
@Order
Note: 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@Priority
Annotation: 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 priority
The main sorting mechanisms includeOrdered
interface,PriorityOrdered
interface,@Order
Annotations and@Priority
annotation. 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!