如果有多个具有相同 @Order 值的 beans 会发生什么

What happens if there are multiple beans having the same @Order value

我有两个 @ControllerAdvice class。一个在应用层,另一个在依赖层。 @ControllerAdvice 都有@Order(Ordered.HIGHEST_PRECEDENCE) 注解。 哪个 bean 将被拾取(首先)进行创建?

应用程序内部的控制器建议:

package com.package.one.errors

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ControllerAdvice1 {}

来自依赖项的控制器建议:

package com.package.two.errors

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ControllerAdvice2 {}

当我运行 Spring 引导应用程序时,正在使用ControllerAdvice1。我怀疑这与扫描时首先遇到的 bean 有关,因为 class 和 @SpringBootApplicationControllerAdvice1 在同一个包内。我想知道如何 Spring 解决这个问题的规则。

来自 API 参考:Ordered.getOrder()

Same order values will result in arbitrary sort positions for the affected objects

还有

The actual order can be interpreted as prioritization, with the first object (with the lowest order value) having the highest priority.

如果有多个组件具有相同的顺序值,Spring 将按照它们的默认出现顺序执行它们。因此,ControllerAdvice1ControllerAdvice2 将按 Spring 的默认顺序 运行。