Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The allowEagerInit parameter of getBeanNamesForType did not work as expected #33740

Open
pop1213 opened this issue Oct 18, 2024 · 1 comment
Open
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Milestone

Comments

@pop1213
Copy link

pop1213 commented Oct 18, 2024

spring version is 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 test results, it was not

test code like this

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 like this

    @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-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 18, 2024
@jhoeller jhoeller added the in: core Issues in core modules (aop, beans, core, context, expression) label Oct 18, 2024
@jhoeller
Copy link
Contributor

Note that eager initialization refers to the "factory-bean" as the bean containing the factory method - which may be in turn an implementation of the FactoryBean interface. Only in such a scenario where it needs to be initialized in order to have a reliable factory method to introspect, it will initialize that target bean then. However, it will not perform eager calls to a factory method just to find out whether the runtime bean instance produced by it has a more specific type than what the method return type declares. I'll revise the getBeanNamesForType javadoc to be more specific about the actual effect there.

@jhoeller jhoeller added type: documentation A documentation task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 18, 2024
@jhoeller jhoeller self-assigned this Oct 18, 2024
@jhoeller jhoeller added this to the 6.1.15 milestone Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

3 participants