Skip to content

Multiple bean instances are created when no id is specified [SPR-11757] #16379

Closed

Description

@spring-projects-issues

Victor Perepelitsky opened SPR-11757 and commented

When importing the same context more then once and there is a bean defined without id it will be created (and defined) multiple times.

In big application it is common case since there are a lot of configuration files that depends on each other and it should be acceptable that the same config is imported more than once.

Attached full test that reproduces the BUG.


Affects: 3.2.6, 3.2.7, 3.2.8, 4.0 GA, 4.0.1, 4.0.2, 4.0.3, 4.0.5

Attachments:

Issue Links:

Activity

  1. spring-projects-issues commented on May 6, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Stéphane Nicoll commented

    I get what you mean but the idea is that if you want to ensure the uniqueness of a bean definition, you have to provide an id for it. It's the same general idea if a bean definition needs to be overridden as the overriding mechanism works through the use of the id.

    When you don't specify any id at all, one unique is generated for you so if the same context is loaded multiple times, a bean with no id will be declared multiple times as well. Check the doc for more details:
    http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-definition

  2. spring-projects-issues commented on May 6, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Victor Perepelitsky commented

    From the reference:
    "You are not required to supply a name or id for a bean. If no name or id is supplied explicitly, the container generates a unique name for that bean. However, if you want to refer to that bean by name, through the use of the ref element or Service Locator style lookup, you must provide a name. Motivations for not supplying a name are related to using inner beans and autowiring collaborators."

    If so I must supply the id only if I want to refer it within the context.
    I still insist it is a real problem and very non intuitive.
    What I showed in my case is not multiple contexts but multiple configuration files that must be merged by Spring to a single context. Which is done and works fine except this specific case.
    Moreover when we define PropertiesPlaceholderConfigurer it usually done without providing an id so multiple unnecessary beans will be created and work.

  3. spring-projects-issues commented on May 6, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Victor Perepelitsky commented

    http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-definition
    From the reference:
    "You are not required to supply a name or id for a bean. If no name or id is supplied explicitly, the container generates a unique name for that bean. However, if you want to refer to that bean by name, through the use of the ref element or Service Locator style lookup, you must provide a name. Motivations for not supplying a name are related to using inner beans and autowiring collaborators."

    If so I must supply the id only if I want to refer it within the context.
    I still insist it is a real problem and very non intuitive.
    What I showed in my case is not multiple contexts but multiple configuration files that must be merged by Spring to a single context. Which is done and works fine except this specific case.
    Moreover when we define PropertiesPlaceholderConfigurer it usually done without providing an id so multiple unnecessary beans will be created and work.

  4. spring-projects-issues commented on May 6, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    This is unfortunately a fundamental limitation at this point: We don't check resource imports for duplicates, since this is rather non-trivial in practice. See #14160 and further related issues.

    The effect that you're seeing extra beans created for unnamed bean definitions naturally comes with those repeated imports. One could argue that the id generation is the problem there but we're not making any guarantees about unique ids for multiple bean definition registration attempts; we just keep the id unique for each registered bean definition.

    From that perspective, the root of all evil is the fact that imports are processed twice, so we'll consider your issue along with the general enhancement in #14160.

    Juergen

  5. spring-projects-issues commented on May 7, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Victor Perepelitsky commented

    Thanks for quick response.
    Are you planning to fix #14160? When?

  6. added
    type: bug
    in: core
    status: duplicate
    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

in: corestatus: duplicate

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions