Introduce findAnnotatedBeans(ListableBeanFactory) in ControllerAdviceBean

This commit introduces a new findAnnotatedBeans(ListableBeanFactory)
override for the existing findAnnotatedBeans(ApplicationContext) method.

Closes gh-35571
This commit is contained in:
Sam Brannen 2025-10-07 12:37:07 +02:00
parent 6aeb9d16e8
commit ff9a349271
1 changed files with 16 additions and 4 deletions

View File

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