Restore lenient target type handling for FactoryBean definitions

Closes gh-23561
This commit is contained in:
Juergen Hoeller
2019-09-20 21:54:32 +02:00
parent a48c13ae73
commit e68132686d
5 changed files with 77 additions and 18 deletions

View File

@@ -16,7 +16,6 @@
package org.springframework.beans.factory;
import org.springframework.core.AttributeAccessor;
import org.springframework.lang.Nullable;
/**
@@ -61,12 +60,13 @@ public interface FactoryBean<T> {
/**
* The name of an attribute that can be
* {@link AttributeAccessor#setAttribute set} on a
* {@link org.springframework.core.AttributeAccessor#setAttribute set} on a
* {@link org.springframework.beans.factory.config.BeanDefinition} so that
* factory beans can signal their object type when it can't be deduced from
* the factory bean class.
* @since 5.2
*/
public static final String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";
String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";
/**

View File

@@ -830,9 +830,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
return result;
}
ResolvableType beanType = mbd.hasBeanClass() ?
ResolvableType.forClass(mbd.getBeanClass()) :
ResolvableType.NONE;
ResolvableType beanType =
(mbd.hasBeanClass() ? ResolvableType.forClass(mbd.getBeanClass()) : ResolvableType.NONE);
// For instance supplied beans try the target type and bean class
if (mbd.getInstanceSupplier() != null) {
@@ -2028,8 +2027,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
}
private boolean isFactoryBeanMethod(Method method) {
return method.getName().equals(this.factoryMethodName) &&
FactoryBean.class.isAssignableFrom(method.getReturnType());
return (method.getName().equals(this.factoryMethodName) &&
FactoryBean.class.isAssignableFrom(method.getReturnType()));
}
ResolvableType getResult() {

View File

@@ -1603,7 +1603,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
Boolean result = mbd.isFactoryBean;
if (result == null) {
Class<?> beanType = predictBeanType(beanName, mbd, FactoryBean.class);
result = beanType != null && FactoryBean.class.isAssignableFrom(beanType);
result = (beanType != null && FactoryBean.class.isAssignableFrom(beanType));
mbd.isFactoryBean = result;
}
return result;
@@ -1787,17 +1787,24 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
if (!(beanInstance instanceof FactoryBean)) {
throw new BeanIsNotAFactoryException(beanName, beanInstance.getClass());
}
if (mbd != null) {
mbd.isFactoryBean = true;
}
return beanInstance;
}
// Now we have the bean instance, which may be a normal bean or a FactoryBean.
// If it's a FactoryBean, we use it to create a bean instance, unless the
// caller actually wants a reference to the factory.
if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {
if (!(beanInstance instanceof FactoryBean)) {
return beanInstance;
}
Object object = null;
if (mbd == null) {
if (mbd != null) {
mbd.isFactoryBean = true;
}
else {
object = getCachedObjectForFactoryBean(beanName);
}
if (object == null) {