From ff9a34927133059bef680c26f85129a1021513e9 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Tue, 7 Oct 2025 12:37:07 +0200 Subject: [PATCH] Introduce findAnnotatedBeans(ListableBeanFactory) in ControllerAdviceBean This commit introduces a new findAnnotatedBeans(ListableBeanFactory) override for the existing findAnnotatedBeans(ApplicationContext) method. Closes gh-35571 --- .../web/method/ControllerAdviceBean.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) 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 9a47c295a1..9c30cb0fcf 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 @@ -228,14 +228,26 @@ public class ControllerAdviceBean implements Ordered { * instances. *

Note that the {@code ControllerAdviceBean} instances in the returned list * are sorted using {@link OrderComparator#sort(List)}. + * @see #findAnnotatedBeans(ListableBeanFactory) + */ + public static List findAnnotatedBeans(ApplicationContext context) { + return findAnnotatedBeans((ListableBeanFactory) context); + } + + /** + * Find beans annotated with {@link ControllerAdvice @ControllerAdvice} in the + * given {@link ListableBeanFactory} and wrap them as {@code ControllerAdviceBean} + * instances. + *

Note that the {@code ControllerAdviceBean} instances in the returned list + * are sorted using {@link OrderComparator#sort(List)}. + * @since 7.0 * @see #getOrder() * @see OrderComparator * @see Ordered */ - public static List findAnnotatedBeans(ApplicationContext context) { - ListableBeanFactory beanFactory = context; - if (context instanceof ConfigurableApplicationContext cac) { - // Use internal BeanFactory for potential downcast to ConfigurableBeanFactory above + public static List findAnnotatedBeans(ListableBeanFactory beanFactory) { + if (beanFactory instanceof ConfigurableApplicationContext cac) { + // Use internal BeanFactory for potential downcast to ConfigurableBeanFactory in getOrder(). beanFactory = cac.getBeanFactory(); } List adviceBeans = new ArrayList<>();