-
Notifications
You must be signed in to change notification settings - Fork 38.2k
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
Autowiring fails if multiple non-highest @Priority
beans exist with same priority
#33733
Comments
@Priority
beans exist with same priority value
Congratulations on submitting your first issue for the Spring Framework! 👍 I have reduced your example to the following standalone test class. @SpringJUnitConfig
class PriorityBeanTests {
@Test
void test(@Autowired Service service) {
assertThat(service).isExactlyInstanceOf(Service1.class);
}
interface Service {
}
@Priority(1)
static class Service1 implements Service {
}
@Priority(2)
static class Service2A implements Service {
}
@Priority(2)
static class Service2B implements Service {
}
@Priority(3)
static class Service3 implements Service {
}
@Configuration
@Import({
Service3.class,
Service2B.class,
Service2A.class,
Service1.class,
})
static class Config {
}
} As you hinted at, that fails with the following exception.
However, if I change the order in which the The following are two such configurations that pass. @Import({
Service1.class,
Service2A.class,
Service2B.class,
Service3.class,
}) @Import({
Service2A.class,
Service3.class,
Service1.class,
Service2B.class,
}) I would consider that a bug in the implementation of However, one could consider it a user error if any beans of the same type are assigned the same priority via |
Yes, I agree with you, but I still think there may exist special cases where different beans have the same priority. |
@Priority
beans exist with same priority value@Priority
beans exist with same priority
This has been fixed in
It turns out a simple boolean flag was all that was needed to implement the check. See the aforementioned commit for details. |
Yes, your solution is clearer and more simple. |
Overview
When I was reading the source code for DefaultListableBeanFactory#determineHighestPriorityCandidate(), I found that when multiple beans have the same priority but not the highest priority, the highest priority bean cannot be obtained correctly.
Example
Consider the following scenario:
Proposal
I know that
@Qualifier
or@Primary
can be used to handle this situation, but in this case, the highest priority bean is onlyFacade3
, which should be correctly injected intoConfig
instead of throwing an exception.Can you consider adding the highest priority results to a
List
(refer to the code below) and throwing an exception if there are multiple beans with the highest priority?The text was updated successfully, but these errors were encountered: