我正在使用 Spring Boot 嵌入式 tomcat。
在我的应用程序中,我有三个配置类,我使用了 @Order
注释来控制类的加载顺序。当我在嵌入式 Tomcat 中运行应用程序时,它工作正常,但在独立 Tomcat (WAR) 中,它没有以正确的顺序加载。
我的类(class)如下:
@Order(1) public Class WebConfig
@Order(2) public Class SwaggerConfig
@Order(3) public Class PlanoutConfig
最佳答案
@Order
定义注释组件的排序顺序,而不是配置类。
我想在嵌入式 Tomcat 模式下,您会受益于副作用。
如果你的类是配置类,也就是类
用 @Configuration
注释,spring boot documentation指出
你应该更喜欢 @AutoconfigureOrder
而不是 @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.
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...).
你可以这么写:
@AutoConfigureOrder(0) public Class WebConfig {...}
@AutoConfigureOrder(1) public Class SwaggerConfig {...}
@AutoConfigureOrder(2) public Class PlanoutConfig {...}
关于java - Spring @Order 注释未按正确顺序加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47197575/