parent
d7d474f658
commit
fc42ca2866
|
|
@ -54,7 +54,8 @@ public class ControllerAdviceBean implements Ordered {
|
||||||
*/
|
*/
|
||||||
private final Object beanOrName;
|
private final Object beanOrName;
|
||||||
|
|
||||||
private final boolean isSingletonBean;
|
private final boolean isSingleton;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reference to the resolved bean instance, potentially lazily retrieved
|
* Reference to the resolved bean instance, potentially lazily retrieved
|
||||||
* via the {@code BeanFactory}.
|
* via the {@code BeanFactory}.
|
||||||
|
|
@ -81,11 +82,11 @@ public class ControllerAdviceBean implements Ordered {
|
||||||
public ControllerAdviceBean(Object bean) {
|
public ControllerAdviceBean(Object bean) {
|
||||||
Assert.notNull(bean, "Bean must not be null");
|
Assert.notNull(bean, "Bean must not be null");
|
||||||
this.beanOrName = bean;
|
this.beanOrName = bean;
|
||||||
|
this.isSingleton = true;
|
||||||
this.resolvedBean = bean;
|
this.resolvedBean = bean;
|
||||||
this.beanType = ClassUtils.getUserClass(bean.getClass());
|
this.beanType = ClassUtils.getUserClass(bean.getClass());
|
||||||
this.beanTypePredicate = createBeanTypePredicate(this.beanType);
|
this.beanTypePredicate = createBeanTypePredicate(this.beanType);
|
||||||
this.beanFactory = null;
|
this.beanFactory = null;
|
||||||
this.isSingletonBean = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -117,7 +118,7 @@ public class ControllerAdviceBean implements Ordered {
|
||||||
"] does not contain specified controller advice bean '" + beanName + "'");
|
"] does not contain specified controller advice bean '" + beanName + "'");
|
||||||
|
|
||||||
this.beanOrName = beanName;
|
this.beanOrName = beanName;
|
||||||
this.isSingletonBean = beanFactory.isSingleton(beanName);
|
this.isSingleton = beanFactory.isSingleton(beanName);
|
||||||
this.beanType = getBeanType(beanName, beanFactory);
|
this.beanType = getBeanType(beanName, beanFactory);
|
||||||
this.beanTypePredicate = (controllerAdvice != null ? createBeanTypePredicate(controllerAdvice) :
|
this.beanTypePredicate = (controllerAdvice != null ? createBeanTypePredicate(controllerAdvice) :
|
||||||
createBeanTypePredicate(this.beanType));
|
createBeanTypePredicate(this.beanType));
|
||||||
|
|
@ -191,13 +192,19 @@ public class ControllerAdviceBean implements Ordered {
|
||||||
* Get the bean instance for this {@code ControllerAdviceBean}, if necessary
|
* Get the bean instance for this {@code ControllerAdviceBean}, if necessary
|
||||||
* resolving the bean name through the {@link BeanFactory}.
|
* resolving the bean name through the {@link BeanFactory}.
|
||||||
* <p>As of Spring Framework 5.2, once the bean instance has been resolved it
|
* <p>As of Spring Framework 5.2, once the bean instance has been resolved it
|
||||||
* will be cached, thereby avoiding repeated lookups in the {@code BeanFactory}.
|
* will be cached if it is a singleton, thereby avoiding repeated lookups in
|
||||||
|
* the {@code BeanFactory}.
|
||||||
*/
|
*/
|
||||||
public Object resolveBean() {
|
public Object resolveBean() {
|
||||||
if (!this.isSingletonBean || this.resolvedBean == null) {
|
if (this.resolvedBean == null) {
|
||||||
// this.beanOrName must be a String representing the bean name if
|
// this.beanOrName must be a String representing the bean name if
|
||||||
// this.resolvedBean is null.
|
// this.resolvedBean is null.
|
||||||
this.resolvedBean = obtainBeanFactory().getBean((String) this.beanOrName);
|
Object resolvedBean = obtainBeanFactory().getBean((String) this.beanOrName);
|
||||||
|
// Don't cache non-singletons (e.g., prototypes).
|
||||||
|
if (!this.isSingleton) {
|
||||||
|
return resolvedBean;
|
||||||
|
}
|
||||||
|
this.resolvedBean = resolvedBean;
|
||||||
}
|
}
|
||||||
return this.resolvedBean;
|
return this.resolvedBean;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -207,19 +207,18 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void prototypePageControllerAdvice() throws Exception {
|
public void prototypeControllerAdvice() throws Exception {
|
||||||
this.webAppContext.registerPrototype("maa", ModelAttributeAdvice.class);
|
this.webAppContext.registerPrototype("maa", ModelAttributeAdvice.class);
|
||||||
this.webAppContext.refresh();
|
this.webAppContext.refresh();
|
||||||
|
|
||||||
HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
|
HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
|
||||||
this.handlerAdapter.afterPropertiesSet();
|
this.handlerAdapter.afterPropertiesSet();
|
||||||
ModelAndView mav1 = this.handlerAdapter.handle(this.request, this.response, handlerMethod);
|
Map<String, Object> model1 = this.handlerAdapter.handle(this.request, this.response, handlerMethod).getModel();
|
||||||
ModelAndView mav2 = this.handlerAdapter.handle(this.request, this.response, handlerMethod);
|
Map<String, Object> model2 = this.handlerAdapter.handle(this.request, this.response, handlerMethod).getModel();
|
||||||
|
|
||||||
assertThat(mav1.getModel().get("modelAttributeAdviceInstance")).isNotEqualTo(mav2.getModel().get("modelAttributeAdviceInstance"));
|
assertThat(model1.get("instance")).isNotSameAs(model2.get("instance"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void modelAttributeAdviceInParentContext() throws Exception {
|
public void modelAttributeAdviceInParentContext() throws Exception {
|
||||||
StaticWebApplicationContext parent = new StaticWebApplicationContext();
|
StaticWebApplicationContext parent = new StaticWebApplicationContext();
|
||||||
|
|
@ -336,7 +335,7 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
public void addAttributes(Model model) {
|
public void addAttributes(Model model) {
|
||||||
model.addAttribute("attr1", "gAttr1");
|
model.addAttribute("attr1", "gAttr1");
|
||||||
model.addAttribute("attr2", "gAttr2");
|
model.addAttribute("attr2", "gAttr2");
|
||||||
model.addAttribute("modelAttributeAdviceInstance", this);
|
model.addAttribute("instance", this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue