Mark Paluch opened SPR-16917 and commented
Currently, BeanFactoryUtils.beanNamesForTypeIncludingAncestors(…)
does not return bean names for FactoryBean
s that are properly typed with generics if allowEagerInit
is set to false
. It would be helpful, when properly typed (non-Object
generics) FactoryBean
s would be parsed for their type. See a reproducer below.
This ticket originates in DATACASS-564 where allowEagerInit
is set to true
and context reload fails due to a non-resolved property placeholder in the bean class name.
Beans:
public class BeanRegisterPostProcessorLookupTest {
@Test
public void test() {
GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.load(BeanRegisterPostProcessorLookupTest.class, "BeanRegisterPostProcessorLookupTest.xml");
context.refresh();
}
interface MySession {
}
private static class MySessionFactory extends AbstractFactoryBean<MySession> {
@Override
public Class<?> getObjectType() {
return MySession.class;
}
@Override
protected MySession createInstance() {
return mock(MySession.class);
}
}
private static class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException {
String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, MySession.class, true, false);
assertThat(names, arrayWithSize(2));
}
}
}
XML declaration:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="factory-1"
class="com.example.BeanRegisterPostProcessorLookupTest.MySessionFactory"/>
<bean id="factory-2"
class="com.example.BeanRegisterPostProcessorLookupTest.MySessionFactory"/>
<bean id="bpp"
class="com.example.BeanRegisterPostProcessorLookupTest.MyBeanFactoryPostProcessor"/>
</beans>
Issue Links: - DATACASS-564 Placeholders not replaced in bean definition during postprocessing
Comment From: snicoll
Sorry this got overlooked and thank you for the sample. I did upgrade the sample to Spring Framework 6 and it's working fine for me. I can't link specifically to the change that made it work though.