Skip to content

DefaultSingletonBeanRegistry::getSingleton(String beanName) attempts to create bean when it shouldn't [SPR-10674] #15302

Closed

Description

@spring-projects-issues

manny veloso opened SPR-10674 and commented

According to the docs, getSingleton(String beanName) is not supposed to attempt to create the singleton if it doesn't exist.

"Only checks already instantiated singletons; does not return an Object for singleton bean definitions which have not been instantiated yet."

However, the code calls getSingleton(beanName, true). Calling getSingleton(beanName, true) attempts to create the bean if not found in the registry.

Note that a quick search through the spring sources show that the usage of getSingleton(String beanName) is consistent with the documentation ie: code that uses getSingleton(beanName) attempts to create the bean if null is returned from the function. In addition, one other use checks the existence of the bean in the registry before trying to retriev eit.


Affects: 3.1.4, 3.2.3

Issue Links:

Activity

  1. spring-projects-issues commented on Jun 23, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Phil Webb commented

    Looks like the fix to #14452 might be the problem here:

    f6209cd#commitcomment-3485152

  2. spring-projects-issues commented on Oct 27, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Note that getSingleton only allows for access to "early bean references", i.e. early access to a bean if it happens to be part of a circular reference. There is an explicit check that we're only attempting this for beans which are currently "in creation": This means that the bean kind-of exists already, its instance just hasn't been made available yet since it isn't fully constructed yet - and therefore only made available if a circular reference requires it.

    In that sense, the present code is fine as-is for the intended use cases, including the getSingleton(String) variant, to the best of my understanding.

    Juergen

  3. added
    type: bug
    status: invalid
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

status: invalid

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions