Skip to content

Commit

Permalink
Restore lenient target type handling for FactoryBean definitions
Browse files Browse the repository at this point in the history
Closes gh-23561
  • Loading branch information
jhoeller committed Sep 20, 2019
1 parent a48c13a commit e681326
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 18 deletions.
Expand Up @@ -16,7 +16,6 @@

package org.springframework.beans.factory;

import org.springframework.core.AttributeAccessor;
import org.springframework.lang.Nullable;

/**
Expand Down Expand Up @@ -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";


/**
Expand Down
Expand Up @@ -830,9 +830,8 @@ protected ResolvableType getTypeForFactoryBean(String beanName, RootBeanDefiniti
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) {
Expand Down Expand Up @@ -2028,8 +2027,8 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
}

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() {
Expand Down
Expand Up @@ -1603,7 +1603,7 @@ protected boolean isFactoryBean(String beanName, RootBeanDefinition mbd) {
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;
Expand Down Expand Up @@ -1787,17 +1787,24 @@ protected Object getObjectForBeanInstance(
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) {
Expand Down
Expand Up @@ -332,25 +332,61 @@ public void individualNamedBeanWithMixedConstructorArguments() {
@Test
public void individualBeanWithFactoryBeanSupplier() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.registerBean("fb", TypedFactoryBean.class, TypedFactoryBean::new, bd -> bd.setLazyInit(true));
context.registerBean("fb", NonInstantiatedFactoryBean.class, NonInstantiatedFactoryBean::new, bd -> bd.setLazyInit(true));
context.refresh();

assertThat(context.getType("fb")).isEqualTo(String.class);
assertThat(context.getType("&fb")).isEqualTo(TypedFactoryBean.class);
assertThat(context.getType("&fb")).isEqualTo(NonInstantiatedFactoryBean.class);
}

@Test
public void individualBeanWithFactoryBeanSupplierAndTargetType() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
RootBeanDefinition bd = new RootBeanDefinition();
bd.setInstanceSupplier(TypedFactoryBean::new);
bd.setInstanceSupplier(NonInstantiatedFactoryBean::new);
bd.setTargetType(ResolvableType.forClassWithGenerics(FactoryBean.class, String.class));
bd.setLazyInit(true);
context.registerBeanDefinition("fb", bd);
context.refresh();

assertThat(context.getType("fb")).isEqualTo(String.class);
assertThat(context.getType("&fb")).isEqualTo(FactoryBean.class);
assertThat(context.getBeanNamesForType(FactoryBean.class).length).isEqualTo(1);
assertThat(context.getBeanNamesForType(NonInstantiatedFactoryBean.class).length).isEqualTo(0);
}

@Test
public void individualBeanWithFactoryBeanObjectTypeAsTargetType() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
RootBeanDefinition bd = new RootBeanDefinition();
bd.setBeanClass(TypedFactoryBean.class);
bd.setTargetType(String.class);
context.registerBeanDefinition("fb", bd);
context.refresh();

assertThat(context.getType("&fb")).isEqualTo(TypedFactoryBean.class);
assertThat(context.getType("fb")).isEqualTo(String.class);
assertThat(context.getBeanNamesForType(FactoryBean.class).length).isEqualTo(1);
assertThat(context.getBeanNamesForType(TypedFactoryBean.class).length).isEqualTo(1);
}

@Test
public void individualBeanWithFactoryBeanObjectTypeAsTargetTypeAndLazy() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
RootBeanDefinition bd = new RootBeanDefinition();
bd.setBeanClass(TypedFactoryBean.class);
bd.setTargetType(String.class);
bd.setLazyInit(true);
context.registerBeanDefinition("fb", bd);
context.refresh();

assertThat(context.getType("&fb")).isNull();
assertThat(context.getType("fb")).isEqualTo(String.class);
assertThat(context.getBean("&fb") instanceof FactoryBean);
assertThat(context.getType("&fb")).isEqualTo(TypedFactoryBean.class);
assertThat(context.getType("fb")).isEqualTo(String.class);
assertThat(context.getBeanNamesForType(FactoryBean.class).length).isEqualTo(1);
assertThat(context.getBeanNamesForType(TypedFactoryBean.class).length).isEqualTo(1);
}


Expand Down Expand Up @@ -426,9 +462,9 @@ public BeanB() {

static class BeanC {}

static class TypedFactoryBean implements FactoryBean<String> {
static class NonInstantiatedFactoryBean implements FactoryBean<String> {

public TypedFactoryBean() {
public NonInstantiatedFactoryBean() {
throw new IllegalStateException();
}

Expand All @@ -448,6 +484,24 @@ public boolean isSingleton() {
}
}

static class TypedFactoryBean implements FactoryBean<String> {

@Override
public String getObject() {
return "";
}

@Override
public Class<?> getObjectType() {
return String.class;
}

@Override
public boolean isSingleton() {
return true;
}
}

static class UntypedFactoryBean implements FactoryBean<Object> {

@Override
Expand Down
Expand Up @@ -194,8 +194,7 @@ static class AttributeClassConfiguration {
static class AttributeClassRegistrar implements ImportBeanDefinitionRegistrar {

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
BeanDefinitionRegistry registry) {
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
BeanDefinition definition = BeanDefinitionBuilder.genericBeanDefinition(
RawWithAbstractObjectTypeFactoryBean.class).getBeanDefinition();
definition.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, MyBean.class);
Expand Down

0 comments on commit e681326

Please sign in to comment.