diff --git a/spring-web/src/main/java/org/springframework/web/bind/annotation/ControllerAdvice.java b/spring-web/src/main/java/org/springframework/web/bind/annotation/ControllerAdvice.java index dd59a414555..db1f6c6e4cf 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/annotation/ControllerAdvice.java +++ b/spring-web/src/main/java/org/springframework/web/bind/annotation/ControllerAdvice.java @@ -33,15 +33,15 @@ import org.springframework.stereotype.Component; * multiple {@code @Controller} classes. * *
Classes with {@code @ControllerAdvice} can be declared explicitly as Spring - * beans or auto-detected via classpath scanning. All such beans are sorted via - * {@link org.springframework.core.annotation.AnnotationAwareOrderComparator - * AnnotationAwareOrderComparator}, based on - * {@link org.springframework.core.annotation.Order @Order} and - * {@link org.springframework.core.Ordered Ordered}, and applied in that order - * at runtime. For handling exceptions, an {@code @ExceptionHandler} will be - * picked on the first advice with a matching exception handler method. For - * model attributes and {@code InitBinder} initialization, {@code @ModelAttribute} - * and {@code @InitBinder} methods will also follow {@code @ControllerAdvice} order. + * beans or auto-detected via classpath scanning. All such beans are sorted based + * on {@link org.springframework.core.Ordered Ordered}, + * {@link org.springframework.core.annotation.Order @Order}, and + * {@link javax.annotation.Priority @Priority} (in that order of precedence), + * and applied in that order at runtime. For handling exceptions, an + * {@code @ExceptionHandler} will be picked on the first advice with a matching + * exception handler method. For model attributes and {@code InitBinder} + * initialization, {@code @ModelAttribute} and {@code @InitBinder} methods will + * also follow {@code @ControllerAdvice} order. * *
Note: For {@code @ExceptionHandler} methods, a root exception match will be * preferred to just matching a cause of the current exception, among the handler diff --git a/spring-web/src/main/java/org/springframework/web/method/ControllerAdviceBean.java b/spring-web/src/main/java/org/springframework/web/method/ControllerAdviceBean.java index 5308092ad73..0c54d039fab 100644 --- a/spring-web/src/main/java/org/springframework/web/method/ControllerAdviceBean.java +++ b/spring-web/src/main/java/org/springframework/web/method/ControllerAdviceBean.java @@ -67,7 +67,8 @@ public class ControllerAdviceBean implements Ordered { @Nullable private final BeanFactory beanFactory; - private final int order; + @Nullable + private Integer order; /** @@ -81,7 +82,6 @@ public class ControllerAdviceBean implements Ordered { this.beanType = ClassUtils.getUserClass(bean.getClass()); this.beanTypePredicate = createBeanTypePredicate(this.beanType); this.beanFactory = null; - this.order = initOrderFromBean(bean); } /** @@ -117,16 +117,32 @@ public class ControllerAdviceBean implements Ordered { this.beanTypePredicate = (controllerAdvice != null ? createBeanTypePredicate(controllerAdvice) : createBeanTypePredicate(this.beanType)); this.beanFactory = beanFactory; - this.order = initOrderFromBeanType(this.beanType); } /** - * Return the order value extracted from the {@link ControllerAdvice} - * annotation, or {@link Ordered#LOWEST_PRECEDENCE} otherwise. + * Get the order value for the contained bean. + *
As of Spring Framework 5.2, the order value is lazily retrieved using + * the following algorithm and cached. + *