Skip to content

Lookup of beans of type fails for factory beans if type prediction is used. [SPR-10517] #15148

@spring-projects-issues

Description

@spring-projects-issues

Oliver Drotbohm opened SPR-10517 and commented

Assume the following FactoryBean implementation:

class FactoryBeanImpl implements FactoryBean, MyInterface { … }

Beyond that, assume a BeanPostProcessor implementation that predicts the bean type of that FactoryBean to an arbitrary different type X. Now assume the following lookup code:

String[] names = beanFactory.getBeanNamesOfType(MyInterface.class);

This call returns an empty array if the bean instance for FactoryBeanImpl has not been created, but returns the name of the bean (as expected) once the instance has been created.

The reason for that behavior seems to start in DefaultListableBeanFactory.doGetBeanNamesForType(…), at the line where we try to find the match for the factory bean name. This code calls {{isTypeMatch(…). The first step it does is trying to lookup an already existing instance. In our second case (bean has already been created before) we run in this if clause and finally end up in the ClassUtils.isAssignableValue(typeToMatch, beanInstance) call, which results int true being returned.

If no instance has been created we run down until the call to predictBeanType(…). This call will set beanClass to the predicted type so that the type match is done against the predicted bean type although we're testing the factory dereference explicitly. IMO the code has to be changed to:

Class<?> beanClass = BeanFactoryUtils.isFactoryDereference(name) ? resolveBeanClass(mbd, beanName, FactoryBean.class) : predictBeanType(beanName, mbd, FactoryBean.class, typeToMatch);

Affects: 3.1.4, 3.2.2

Issue Links:

Referenced from: commits bc3e585, ef363b0

0 votes, 5 watchers

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions