spring-projects / spring-projects/spring-framework

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

Open
#36,781 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing Spring's autowiring candidate resolution and how it predicts FactoryBean product types; the issue specifically highlights JndiObjectFactoryBean and beans excluded by default-autowire-candidates. Determine whether excluded FactoryBeans can avoid eager instantiation while resolving FooBar, and add a regression test showing that unrelated, non-candidate factory beans remain lazy during wiring.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.