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:
Sam Brannen
2019-11-19 14:20:19 +01:00
parent 9a522946a5
commit ffcd83e3a8
3 changed files with 143 additions and 10 deletions

View File

@@ -48,17 +48,12 @@ public class RequestScopedControllerAdviceIntegrationTests {
context.register(Config.class);
context.refresh();
// Until gh-24017 is fixed, we expect the RequestScopedControllerAdvice to show up twice.
List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(context);
assertEquals(2, adviceBeans.size());
assertEquals(1, adviceBeans.size());
ControllerAdviceBean adviceBean1 = adviceBeans.get(0);
assertEquals(RequestScopedControllerAdvice.class, adviceBean1.getBeanType());
assertEquals(42, adviceBean1.getOrder());
ControllerAdviceBean adviceBean2 = adviceBeans.get(1);
assertEquals(RequestScopedControllerAdvice.class, adviceBean2.getBeanType());
assertEquals(42, adviceBean2.getOrder());
ControllerAdviceBean adviceBean = adviceBeans.get(0);
assertEquals(RequestScopedControllerAdvice.class, adviceBean.getBeanType());
assertEquals(42, adviceBean.getOrder());
context.close();
}