Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
spring-projects-issues opened this issue Apr 20, 2015 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Apr 20, 2015

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:

@spring-projects-issues
Copy link
Collaborator Author

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

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Apr 24, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

2 participants