Skip to content

Regression: @Primary annotation not working when overriding bean definition with classpath scanning [SPR-12934] #17527

Closed

Description

@spring-projects-issues

Daniel Hiller opened SPR-12934 and commented

The \@Primary annotation seemed to stop working as expected since Spring 4.1.0.RELEASE (including 4.1.6.RELEASE) as demonstrated in this stripped down github repo.

In the repository mentioned we created a stripped down version of our situation to demonstrate the problem. To get a quick start please have a look at the Application classes.

Run

mvn clean install && cd overridden-bean && mvn exec:java

for a demonstration. An execption should state that another bean type was expected.

NB: This looks like a regression as this is still working with latest Spring 4.0.9.RELEASE. Change the spring version in parent pom to 4.0.9.RELEASE, run the above command and see that it works as expected.

Versions used:

  • Apache Maven 3.2.3
  • Java 1.8.0_45
  • Ubuntu 14.04 LTS

Interestingly, when trying to convert this into spring unit tests (see branch convert-to-unit-tests) and then running the tests from either Intellij vs. maven I get a non deterministic behavior (i.e. running from Intellij fails the test, while mvn clean install does not fail the tests)


Affects: 4.1 GA, 4.1.6

Issue Links:

Activity

  1. spring-projects-issues commented on Apr 24, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    This looks like you're relying on the order of classpath detection which is highly fragile, in particular for overriding beans by name....

    You need to make sure that your overriding @Configuration class is being processed after your original configuration class: either through registering them specifically (i.e. listing the configuration class names in individual XML <bean> definitions or registering them in the right order against an ApplicationConfigApplicationContext) or through specifying @Order values on the configuration classes themselves (e.g. @Order(0) on the original and @Order(1) on the overriding class; available as of 4.2).

    Note that @Primary itself does not affect bean definition overriding at all. It just serves as a hint for injection by type when multiple beans of the same type have been registered, not when beans override each other. Try dropping the specific 'myBean' name from those definitions (so that they'll be registered next to each other with their method names as bean names) and you'll see that getBean(MyBean.class) still works. However, for direct references by name, that arrangement might not be what you want.

    Let's reopen this issue if the above doesn't help.

    Juergen

  2. spring-projects-issues commented on Apr 24, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Note that we have a more general issue (#15434) for a wider revision of overriding behavior. I'll just mark this issue here as 'invalid' for its regression character since such an arrangement wasn't meant to reliably work before.

    Juergen

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

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions