Support scoped @ControllerAdvice beans again
Spring Framework 5.2 introduced support for implementing the Ordered interface in a @ControllerAdvice bean. This support requires that @ControllerAdvice beans be eagerly resolved from the BeanFactory in order to invoke the getOrder() method defined in the Ordered interface. Unfortunately doing so resulted in a regression in that an attempt to eagerly resolve a scoped @ControllerAdvice bean throws a BeanCreationException due to the lack of an active scope (e.g., request or session scope). This commit fixes this regression by avoiding eager resolution of scoped @ControllerAdvice beans. As a direct consequence, the Ordered interface is not supported for scoped @ControllerAdvice beans. Closes gh-23985
This commit is contained in:
@@ -42,10 +42,12 @@ import org.springframework.stereotype.Component;
|
||||
* Note, however, that {@code @ControllerAdvice} beans that implement
|
||||
* {@link org.springframework.core.PriorityOrdered PriorityOrdered} are <em>not</em>
|
||||
* given priority over {@code @ControllerAdvice} beans that implement {@code Ordered}.
|
||||
* 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.
|
||||
* In addition, {@code Ordered} is not honored for scoped {@code @ControllerAdvice}
|
||||
* beans — for example if such a bean has been configured as a request-scoped
|
||||
* or session-scoped bean. For handling exceptions, an {@code @ExceptionHandler}
|
||||
* will be picked on the first advice with a matching exception handler method. For
|
||||
* model attributes and data binding initialization, {@code @ModelAttribute} and
|
||||
* {@code @InitBinder} methods will follow {@code @ControllerAdvice} order.
|
||||
*
|
||||
* <p>Note: For {@code @ExceptionHandler} methods, a root exception match will be
|
||||
* preferred to just matching a cause of the current exception, among the handler
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.web.method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.aop.scope.ScopedProxyUtils;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -124,19 +125,42 @@ public class ControllerAdviceBean implements Ordered {
|
||||
/**
|
||||
* Get the order value for the contained bean.
|
||||
* <p>As of Spring Framework 5.2, the order value is lazily retrieved using
|
||||
* the following algorithm and cached.
|
||||
* the following algorithm and cached. Note, however, that a
|
||||
* {@link ControllerAdvice @ControllerAdvice} bean that is configured as a
|
||||
* scoped bean — for example, as a request-scoped or session-scoped
|
||||
* bean — will not be eagerly resolved. Consequently, {@link Ordered} is
|
||||
* not honored for scoped {@code @ControllerAdvice} beans.
|
||||
* <ul>
|
||||
* <li>If the {@linkplain #resolveBean resolved bean} implements {@link Ordered},
|
||||
* use the value returned by {@link Ordered#getOrder()}.</li>
|
||||
* <li>Otherwise use the value returned by {@link OrderUtils#getOrder(Class, int)}
|
||||
* with {@link Ordered#LOWEST_PRECEDENCE} used as the default order value.</li>
|
||||
* <li>If the {@linkplain #getBeanType() bean type} is known, use the value returned
|
||||
* by {@link OrderUtils#getOrder(Class, int)} with {@link Ordered#LOWEST_PRECEDENCE}
|
||||
* used as the default order value.</li>
|
||||
* <li>Otherwise use {@link Ordered#LOWEST_PRECEDENCE} as the default, fallback
|
||||
* order value.</li>
|
||||
* </ul>
|
||||
* @see #resolveBean()
|
||||
*/
|
||||
@Override
|
||||
public int getOrder() {
|
||||
if (this.order == null) {
|
||||
Object resolvedBean = resolveBean();
|
||||
Object resolvedBean = null;
|
||||
if (this.beanOrName instanceof String) {
|
||||
String beanName = (String) this.beanOrName;
|
||||
String targetBeanName = ScopedProxyUtils.getTargetBeanName(beanName);
|
||||
boolean isScopedProxy = this.beanFactory.containsBean(targetBeanName);
|
||||
// Avoid eager @ControllerAdvice bean resolution for scoped proxies,
|
||||
// since attempting to do so during context initialization would result
|
||||
// in an exception due to the current absence of the scope. For example,
|
||||
// an HTTP request or session scope is not active during initialization.
|
||||
if (!isScopedProxy && !ScopedProxyUtils.isScopedTarget(beanName)) {
|
||||
resolvedBean = resolveBean();
|
||||
}
|
||||
}
|
||||
else {
|
||||
resolvedBean = resolveBean();
|
||||
}
|
||||
|
||||
if (resolvedBean instanceof Ordered) {
|
||||
this.order = ((Ordered) resolvedBean).getOrder();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user