Skip to content

maybe lazy factory beans that are not candidates for auto wiring should not be eagerly instantiated? #36781

@markyag-spgdji

Description

@markyag-spgdji

Consider:

<?xml version="1.0" encoding="UTF-8"?>
<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
    "
    default-lazy-init="true"
    default-autowire-candidates=""
>
<!-- imagine we enough properties here to make it useful -->
<bean id="lazyInitJndiObjectFactoryBean" class="org.springframework.jndi.JndiObjectFactoryBean"/>
<!-- imagine we have 100 more of these -->

And now, somewhere far away, in a totally unrelated class, we have a totally unrelated field:

@Component
class MyClass {
   @Autowired FooBar fooBar; // trouble
}

When Spring tries to wire up fooBar, it will instantiate every single Factory Bean it can get its hands on, because for all it knows, maybe the factory bean can make FooBar objects. (Usually they "obviously" can't, but it's not obvious to Spring.)

In general, this is hard to avoid, but why do it for beans that are not even candidates for auto wiring?
In this case of JNDI, there's a setting to delay lookups that mitigates the damage, but this is ad-hoc.
Each Factory Bean needs to decide if it wants to support something like that.
Any chance of solving this at the spring level?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions