Skip to content

Commit 9333ed2

Browse files
committed
Avoid repeated FactoryBean targetType check
See gh-30987
1 parent 4b6fabb commit 9333ed2

File tree

3 files changed

+45
-38
lines changed

3 files changed

+45
-38
lines changed

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -837,16 +837,13 @@ protected ResolvableType getTypeForFactoryBean(String beanName, RootBeanDefiniti
837837
return result;
838838
}
839839

840-
ResolvableType beanType =
841-
(mbd.hasBeanClass() ? ResolvableType.forClass(mbd.getBeanClass()) : ResolvableType.NONE);
842-
843-
// For instance supplied beans try the target type and bean class
840+
// For instance supplied beans, try the target type and bean class immediately
844841
if (mbd.getInstanceSupplier() != null) {
845842
result = getFactoryBeanGeneric(mbd.targetType);
846843
if (result.resolve() != null) {
847844
return result;
848845
}
849-
result = getFactoryBeanGeneric(beanType);
846+
result = getFactoryBeanGeneric(mbd.hasBeanClass() ? ResolvableType.forClass(mbd.getBeanClass()) : null);
850847
if (result.resolve() != null) {
851848
return result;
852849
}
@@ -909,22 +906,20 @@ protected ResolvableType getTypeForFactoryBean(String beanName, RootBeanDefiniti
909906
return getTypeForFactoryBeanFromMethod(mbd.getBeanClass(), factoryMethodName);
910907
}
911908

912-
result = getFactoryBeanGeneric(mbd.targetType);
913-
if (result.resolve() != null) {
914-
return result;
915-
}
916-
result = getFactoryBeanGeneric(beanType);
917-
if (result.resolve() != null) {
918-
return result;
909+
// For regular beans, try the target type and bean class as fallback
910+
if (mbd.getInstanceSupplier() == null) {
911+
result = getFactoryBeanGeneric(mbd.targetType);
912+
if (result.resolve() != null) {
913+
return result;
914+
}
915+
result = getFactoryBeanGeneric(mbd.hasBeanClass() ? ResolvableType.forClass(mbd.getBeanClass()) : null);
916+
if (result.resolve() != null) {
917+
return result;
918+
}
919919
}
920-
return ResolvableType.NONE;
921-
}
922920

923-
private ResolvableType getFactoryBeanGeneric(@Nullable ResolvableType type) {
924-
if (type == null) {
925-
return ResolvableType.NONE;
926-
}
927-
return type.as(FactoryBean.class).getGeneric();
921+
// FactoryBean type not resolvable
922+
return ResolvableType.NONE;
928923
}
929924

930925
/**

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
6565
import org.springframework.beans.factory.config.Scope;
6666
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;
67-
import org.springframework.core.AttributeAccessor;
6867
import org.springframework.core.DecoratingClassLoader;
6968
import org.springframework.core.NamedThreadLocal;
7069
import org.springframework.core.ResolvableType;
@@ -1684,25 +1683,8 @@ else if (mbd.isLazyInit()) {
16841683
onSuppressedException(ex);
16851684
}
16861685
}
1687-
return ResolvableType.NONE;
1688-
}
16891686

1690-
/**
1691-
* Determine the bean type for a FactoryBean by inspecting its attributes for a
1692-
* {@link FactoryBean#OBJECT_TYPE_ATTRIBUTE} value.
1693-
* @param attributes the attributes to inspect
1694-
* @return a {@link ResolvableType} extracted from the attributes or
1695-
* {@code ResolvableType.NONE}
1696-
* @since 5.2
1697-
*/
1698-
ResolvableType getTypeForFactoryBeanFromAttributes(AttributeAccessor attributes) {
1699-
Object attribute = attributes.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE);
1700-
if (attribute instanceof ResolvableType resolvableType) {
1701-
return resolvableType;
1702-
}
1703-
if (attribute instanceof Class<?> clazz) {
1704-
return ResolvableType.forClass(clazz);
1705-
}
1687+
// FactoryBean type not resolvable
17061688
return ResolvableType.NONE;
17071689
}
17081690

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
2525
import org.springframework.beans.factory.FactoryBean;
2626
import org.springframework.beans.factory.FactoryBeanNotInitializedException;
27+
import org.springframework.core.AttributeAccessor;
28+
import org.springframework.core.ResolvableType;
2729
import org.springframework.lang.Nullable;
2830

2931
/**
@@ -61,6 +63,34 @@ protected Class<?> getTypeForFactoryBean(FactoryBean<?> factoryBean) {
6163
}
6264
}
6365

66+
/**
67+
* Determine the bean type for a FactoryBean by inspecting its attributes for a
68+
* {@link FactoryBean#OBJECT_TYPE_ATTRIBUTE} value.
69+
* @param attributes the attributes to inspect
70+
* @return a {@link ResolvableType} extracted from the attributes or
71+
* {@code ResolvableType.NONE}
72+
* @since 5.2
73+
*/
74+
ResolvableType getTypeForFactoryBeanFromAttributes(AttributeAccessor attributes) {
75+
Object attribute = attributes.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE);
76+
if (attribute instanceof ResolvableType resolvableType) {
77+
return resolvableType;
78+
}
79+
if (attribute instanceof Class<?> clazz) {
80+
return ResolvableType.forClass(clazz);
81+
}
82+
return ResolvableType.NONE;
83+
}
84+
85+
/**
86+
* Determine the FactoryBean object type from the given generic declaration.
87+
* @param type the FactoryBean type
88+
* @return the nested object type, or {@code NONE} if not resolvable
89+
*/
90+
ResolvableType getFactoryBeanGeneric(@Nullable ResolvableType type) {
91+
return (type != null ? type.as(FactoryBean.class).getGeneric() : ResolvableType.NONE);
92+
}
93+
6494
/**
6595
* Obtain an object to expose from the given FactoryBean, if available
6696
* in cached form. Quick check for minimal synchronization.

0 commit comments

Comments
 (0)