Skip to content

Commit

Permalink
Polish contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Dec 7, 2019
1 parent d7d474f commit fc42ca2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public class ControllerAdviceBean implements Ordered {
*/
private final Object beanOrName;

private final boolean isSingletonBean;
private final boolean isSingleton;

/**
* Reference to the resolved bean instance, potentially lazily retrieved
* via the {@code BeanFactory}.
Expand All @@ -81,11 +82,11 @@ public class ControllerAdviceBean implements Ordered {
public ControllerAdviceBean(Object bean) {
Assert.notNull(bean, "Bean must not be null");
this.beanOrName = bean;
this.isSingleton = true;
this.resolvedBean = bean;
this.beanType = ClassUtils.getUserClass(bean.getClass());
this.beanTypePredicate = createBeanTypePredicate(this.beanType);
this.beanFactory = null;
this.isSingletonBean = true;
}

/**
Expand Down Expand Up @@ -117,7 +118,7 @@ public ControllerAdviceBean(String beanName, BeanFactory beanFactory, @Nullable
"] does not contain specified controller advice bean '" + beanName + "'");

this.beanOrName = beanName;
this.isSingletonBean = beanFactory.isSingleton(beanName);
this.isSingleton = beanFactory.isSingleton(beanName);
this.beanType = getBeanType(beanName, beanFactory);
this.beanTypePredicate = (controllerAdvice != null ? createBeanTypePredicate(controllerAdvice) :
createBeanTypePredicate(this.beanType));
Expand Down Expand Up @@ -191,13 +192,19 @@ public Class<?> getBeanType() {
* Get the bean instance for this {@code ControllerAdviceBean}, if necessary
* resolving the bean name through the {@link BeanFactory}.
* <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() {
if (!this.isSingletonBean || this.resolvedBean == null) {
if (this.resolvedBean == null) {
// this.beanOrName must be a String representing the bean name if
// 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,18 @@ public void modelAttributeAdvice() throws Exception {
}

@Test
public void prototypePageControllerAdvice() throws Exception {
public void prototypeControllerAdvice() 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);
Map<String, Object> model1 = this.handlerAdapter.handle(this.request, this.response, handlerMethod).getModel();
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
public void modelAttributeAdviceInParentContext() throws Exception {
StaticWebApplicationContext parent = new StaticWebApplicationContext();
Expand Down Expand Up @@ -336,7 +335,7 @@ private static class ModelAttributeAdvice {
public void addAttributes(Model model) {
model.addAttribute("attr1", "gAttr1");
model.addAttribute("attr2", "gAttr2");
model.addAttribute("modelAttributeAdviceInstance", this);
model.addAttribute("instance", this);
}
}

Expand Down

0 comments on commit fc42ca2

Please sign in to comment.