Skip to content

Reconsider whether to generally allow bean overriding by name [SPR-10808] #15434

Closed as not planned
@spring-projects-issues

Description

@spring-projects-issues

Emerson Farrugia opened SPR-10808 and commented

I've hit an issue where a single component scan was finding two @Beans with the same name. The beans weren't intentionally named the same, it was just an oversight. The container initialised one bean and ignored the other, logging at info instead of warn. The logging level is the first problem, since logging at info makes it much more likely the message is missed. The second problem is that the ignored bean was explicitly marked as @Primary. I've attached files with an example.

As far as I can tell, there's no way to control which bean gets picked in this situation. @Order on the @Configuration doesn't help.

In terms of fixes, I suggest the info message gets changed to warning. I'd personally even go one further. Given the functionality of @Profile, @Primary, and that autowiring by type is encouraged, does bean overriding still make sense in 3.1+.x? @Profile and @Primary give us a deterministic way of controlling autowire candidates. In contrast, quiet non-deterministic overrides by name seem too fragile to fit.


Affects: 3.2.3

Attachments:

Issue Links:

2 votes, 7 watchers

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: supersededAn issue that has been superseded by anothertype: taskA general task

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions