Skip to content

Improve documentation for allowEagerInit parameter in getBeanNamesForType() #33740

@pop1213

Description

@pop1213

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);
     
    }

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: documentationA documentation task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions