Skip to content

Unexpected @Autowired bean when newly registered bean is set as Primary #28360

@rj-cam

Description

@rj-cam

Affects: Spring 5.3.18

When registering a new bean X2 of type X and marking it as the primary implementation, a previously @Autowired field is still returning the previous instance type X1 when the parent bean instance is constructed.

Expected: The new primary class X2 is injected. The class with the @Autowired field is marked with @scope("prototype").
Actual: The previous class X1 is injected instead.

This code screenshot should be able to explain the issue better:
image

PS. If the parent bean has not been retrieved prior to the bean definition, it works fine. The correct bean X2 is being returned.

https://github.com/rj-cam/autowire-primary/tree/main/src/test/java/com/example/autowireprimary


Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions