Skip to content

Commit

Permalink
Cache AbstractBeanFactory.isFactoryBean results
Browse files Browse the repository at this point in the history
Add an additional cache to the `RootBeanDefinition` to save
recalculating the result of `isFactoryBean`.

Closes spring-projectsgh-23337
  • Loading branch information
philwebb committed Jul 23, 2019
1 parent 3ac8de2 commit 2699738
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,7 @@ private void copyRelevantMergedBeanDefinitionCaches(RootBeanDefinition previous,
ObjectUtils.nullSafeEquals(mbd.getFactoryMethodName(), previous.getFactoryMethodName()) &&
(mbd.targetType == null || mbd.targetType.equals(previous.targetType))) {
mbd.targetType = previous.targetType;
mbd.isFactoryBean = previous.isFactoryBean;
mbd.resolvedTargetType = previous.resolvedTargetType;
mbd.factoryMethodReturnType = previous.factoryMethodReturnType;
mbd.factoryMethodToIntrospect = previous.factoryMethodToIntrospect;
Expand Down Expand Up @@ -1541,8 +1542,13 @@ protected Class<?> predictBeanType(String beanName, RootBeanDefinition mbd, Clas
* @param mbd the corresponding bean definition
*/
protected boolean isFactoryBean(String beanName, RootBeanDefinition mbd) {
Class<?> beanType = predictBeanType(beanName, mbd, FactoryBean.class);
return (beanType != null && FactoryBean.class.isAssignableFrom(beanType));
Boolean result = mbd.isFactoryBean;
if (result == null) {
Class<?> beanType = predictBeanType(beanName, mbd, FactoryBean.class);
result = beanType != null && FactoryBean.class.isAssignableFrom(beanType);
mbd.isFactoryBean = result;
}
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
@Nullable
volatile Class<?> resolvedTargetType;

/** Package-visible field for caching if the bean is a factory bean. */
@Nullable
volatile Boolean isFactoryBean;

/** Package-visible field for caching the return type of a generically typed factory method. */
@Nullable
volatile ResolvableType factoryMethodReturnType;
Expand Down

0 comments on commit 2699738

Please sign in to comment.