Ignore scoped proxy targets for @ControllerAdvice beans
Prior to this commit, methods in a @ControllerAdvice bean were registered and invoked twice if the advice was a scoped bean (e.g., request or session scoped). In other words, both the proxy bean and the target bean were wrapped in ControllerAdviceBean instances. This commit fixes this bug by modifying the findAnnotatedBeans() method in ControllerAdviceBean so that it filters out targets of scoped proxies. Closes gh-24017
This commit is contained in:
@@ -252,11 +252,13 @@ public class ControllerAdviceBean implements Ordered {
|
||||
public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext context) {
|
||||
List<ControllerAdviceBean> adviceBeans = new ArrayList<>();
|
||||
for (String name : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, Object.class)) {
|
||||
ControllerAdvice controllerAdvice = context.findAnnotationOnBean(name, ControllerAdvice.class);
|
||||
if (controllerAdvice != null) {
|
||||
// Use the @ControllerAdvice annotation found by findAnnotationOnBean()
|
||||
// in order to avoid a subsequent lookup of the same annotation.
|
||||
adviceBeans.add(new ControllerAdviceBean(name, context, controllerAdvice));
|
||||
if (!ScopedProxyUtils.isScopedTarget(name)) {
|
||||
ControllerAdvice controllerAdvice = context.findAnnotationOnBean(name, ControllerAdvice.class);
|
||||
if (controllerAdvice != null) {
|
||||
// Use the @ControllerAdvice annotation found by findAnnotationOnBean()
|
||||
// in order to avoid a subsequent lookup of the same annotation.
|
||||
adviceBeans.add(new ControllerAdviceBean(name, context, controllerAdvice));
|
||||
}
|
||||
}
|
||||
}
|
||||
OrderComparator.sort(adviceBeans);
|
||||
|
||||
Reference in New Issue
Block a user