Do not cache prototype @ControllerAdvice beans
Spring Framework 5.2 introduced support for caching @ControllerAdvice beans; however, this caching was also applied incorrectly to non-singleton beans. This commit addresses this regression by only caching singleton @ControllerAdvice beans. Closes gh-24157
This commit is contained in:
@@ -206,6 +206,20 @@ public class RequestMappingHandlerAdapterTests {
|
||||
assertThat(mav.getModel().get("attr2")).isEqualTo("gAttr2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prototypePageControllerAdvice() throws Exception {
|
||||
this.webAppContext.registerPrototype("maa", ModelAttributeAdvice.class);
|
||||
this.webAppContext.refresh();
|
||||
|
||||
HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
|
||||
this.handlerAdapter.afterPropertiesSet();
|
||||
ModelAndView mav1 = this.handlerAdapter.handle(this.request, this.response, handlerMethod);
|
||||
ModelAndView mav2 = this.handlerAdapter.handle(this.request, this.response, handlerMethod);
|
||||
|
||||
assertThat(mav1.getModel().get("modelAttributeAdviceInstance")).isNotEqualTo(mav2.getModel().get("modelAttributeAdviceInstance"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void modelAttributeAdviceInParentContext() throws Exception {
|
||||
StaticWebApplicationContext parent = new StaticWebApplicationContext();
|
||||
@@ -322,6 +336,7 @@ public class RequestMappingHandlerAdapterTests {
|
||||
public void addAttributes(Model model) {
|
||||
model.addAttribute("attr1", "gAttr1");
|
||||
model.addAttribute("attr2", "gAttr2");
|
||||
model.addAttribute("modelAttributeAdviceInstance", this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user