Vaisakh PS
Vaisakh PS

Reputation: 1201

Spring @Order annotation not loading in correct order

I am using Spring Boot embedded tomcat.
In my application I have three configuration classes and I have used the @Order annotation to control the loading order of classes. When I run the application in embedded Tomcat it's working fine, but in standalone Tomcat (WAR) it's not loading in the correct order.
My classes are like below :

@Order(1) public Class WebConfig 
@Order(2) public Class SwaggerConfig
@Order(3) public Class PlanoutConfig

Upvotes: 0

Views: 11005

Answers (1)

davidxxx
davidxxx

Reputation: 131346

@Order defines the sort order for annotated components, not for configuration classes.
I suppose that in embedded Tomcat mode, you benefit from a side effect.

If your classes are configuration classes, that is, classes annotated with @Configuration, the spring boot documentation states that you should favor @AutoconfigureOrder over @Order.

44.1 Understanding auto-configured beans

If you want to order certain auto-configurations that shouldn’t have any direct knowledge of each other, you can also use @AutoconfigureOrder. That annotation has the same semantic as the regular @Order annotation but provides a dedicated order for auto-configuration classes.


AutoConfigureOrder

public @interface AutoConfigureOrder

Auto-configuration specific variant of Spring Framework's Order annotation. Allows auto-configuration classes to be ordered among themselves without affecting the order of configuration classes passed to AnnotationConfigApplicationContext.register(Class...).

You could so write :

@AutoConfigureOrder(0) public Class WebConfig {...}
@AutoConfigureOrder(1) public Class SwaggerConfig {...}
@AutoConfigureOrder(2) public Class PlanoutConfig {...}

Upvotes: 8

Related Questions