Skip to content

Commit 527876d

Browse files
philwebbjhoeller
authored andcommitted
Cache AbstractBeanFactory.isFactoryBean results
Add an additional cache to the `RootBeanDefinition` to save recalculating the result of `isFactoryBean`. Closes gh-23337
1 parent 95edcb8 commit 527876d

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,7 @@ private void copyRelevantMergedBeanDefinitionCaches(RootBeanDefinition previous,
13331333
ObjectUtils.nullSafeEquals(mbd.getFactoryMethodName(), previous.getFactoryMethodName()) &&
13341334
(mbd.targetType == null || mbd.targetType.equals(previous.targetType))) {
13351335
mbd.targetType = previous.targetType;
1336+
mbd.isFactoryBean = previous.isFactoryBean;
13361337
mbd.resolvedTargetType = previous.resolvedTargetType;
13371338
mbd.factoryMethodReturnType = previous.factoryMethodReturnType;
13381339
mbd.factoryMethodToIntrospect = previous.factoryMethodToIntrospect;
@@ -1541,8 +1542,13 @@ protected Class<?> predictBeanType(String beanName, RootBeanDefinition mbd, Clas
15411542
* @param mbd the corresponding bean definition
15421543
*/
15431544
protected boolean isFactoryBean(String beanName, RootBeanDefinition mbd) {
1544-
Class<?> beanType = predictBeanType(beanName, mbd, FactoryBean.class);
1545-
return (beanType != null && FactoryBean.class.isAssignableFrom(beanType));
1545+
Boolean result = mbd.isFactoryBean;
1546+
if (result == null) {
1547+
Class<?> beanType = predictBeanType(beanName, mbd, FactoryBean.class);
1548+
result = beanType != null && FactoryBean.class.isAssignableFrom(beanType);
1549+
mbd.isFactoryBean = result;
1550+
}
1551+
return result;
15461552
}
15471553

15481554
/**

spring-beans/src/main/java/org/springframework/beans/factory/support/RootBeanDefinition.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
7474
@Nullable
7575
volatile Class<?> resolvedTargetType;
7676

77+
/** Package-visible field for caching if the bean is a factory bean. */
78+
@Nullable
79+
volatile Boolean isFactoryBean;
80+
7781
/** Package-visible field for caching the return type of a generically typed factory method. */
7882
@Nullable
7983
volatile ResolvableType factoryMethodReturnType;

0 commit comments

Comments
 (0)