Spring version: 6.0.16
This is the documentation for the ListableBeanFactory#getBeanNamesForType method.
allowEagerInit – whether to initialize lazy-init singletons and objects created by FactoryBeans (or by factory methods with a "factory-bean" reference) for the type check. Note that FactoryBeans need to be eagerly initialized to determine their type: So be aware that passing in "true" for this flag will initialize FactoryBeans and "factory-bean" references.
According to the documentation of the getBeanNamesForType method, the bean myBeanCreater should be eagerly initialized. However, in the following test it was not.
public interface BaseBean {
}
@AllArgsConstructor
public class MyBean implements BaseBean {
private String name;
}
public class MyBeanCreator {
public MyBeanCreator() {
System.out.println("MyBeanCreator initialized");
}
public BaseBean createMyBean(){
return new MyBean("test");
}
}
beans.xml:
<bean id="myBeanCreator" class="com.example.wemvc.MyBeanCreator" lazy-init="true" ></bean>
<bean id="myBean" lazy-init="true" factory-bean="myBeanCreator" factory-method="createMyBean"></bean>
Test:
@Test
public void test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
context.setConfigLocation("beans.xml");
context.refresh();
//allowEagerInit is true and `myBeanCreator `is still not initialized
// beanNamesForType = []
String[] beanNamesForType = context.getBeanNamesForType(MyBean.class, true, true);
}
Spring version: 6.0.16
This is the documentation for the
ListableBeanFactory#getBeanNamesForTypemethod.According to the documentation of the
getBeanNamesForTypemethod, the beanmyBeanCreatershould be eagerly initialized. However, in the following test it was not.beans.xml:
Test: