-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
@Priority(LOWEST_PRECEDENCE) still higher than no priority #31544
Comments
Yes.
I disagree. Not having a priority (or an
If you implement |
but this is not the case here! - if that was the case, there would be an exception thrown here due to two beans having |
Please review the doc once again.
You can have as many component with the same priority value as you want. If they have the same value, the order is arbitrary. There's no after or before as you claim. |
@snicoll with the same
I'm under the impression you're mistaking |
@snicoll even the docs say that it's not what you say:
Point here, it's not that "If you implement Ordered then you must provide a default value." - there is no |
That's just an internal detail of how this is implemented and we're mixing the two indeed but I am trying to explain why this is the expected behavior. Your third component does not have a priority so it passes the check above. At the end of the day, for the ordering purpose, a component that does not have a priority (order) comes "at the end". From an ordering perspective, it means assigning it
Behind the scenes, that's exactly what's happening. Looking at the spec not much is said about what is happening if a component does not have a value but it's totally logical to me that it as a lower priority than the lowest priority that is defined explicitly. |
@snicoll TBH, for me it would be logical if the term "lowest priority" would imply that a thing would have lowest priority of all, and not of just a subset of cases, but I'm not at liberty to argue about logics here. Thank you for your prompt response; if it's by design, then it's by design. |
I believe the current design is consistent. If you don't have an order, then you're invoked after every component that have one. Same for priority. |
@snicoll I could just provide
It's not really that separate, especially taking into account things like https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/PriorityOrdered.html etc. , and taking into account that the implementation of priority-related methods repeatedly talk about ordering, and use methods called as such. E.g.
and from
So yes, I'd say both the docs and the implmentations explicitly link the concepts. |
@snicoll this is also from the docs themselves (AnnotationAwareOrderComparator):
I'm thus, saying "
See above. It's mentioned multiple times over the docs that it's not "a conicidence" they both use integer values; they were intended to be semantically related. I think I provided enough source reference for that.
If that's a "separate concept", you should rewrite all the docs and code you have in Spring - because, as currently stands, it's not separate by any means. They are mentioned in tandem virtually everywhere, the code concerning both overlaps, and the use cases as well - resulting in long-lived confusion; search the internet for how many people believe that |
@jhoeller & @sbrannen are the people who changed the docs to reflect that, are you guys still around and can you chime in on whether see e.g. 1ff3af6#diff-ec91a2069056e7887259843f7f76c280b13d5737e25f9e019bde45e927394d2eR67 , c6d29f1#diff-7647ef528e5ffcfdb8ce8b88d189a906a1e7b46778323bc92cb9783a4991d5abR1225 etc. |
Affects: all Spring Framework versions (
spring-beans
module)@Priority(LOWEST_PRECEDENCE)
is still higher than no priority - which makes it impossible to lower the priority of selected@Component
via this annotation.(see #26241 for a similar issue, although this one proposes a simpler solution for a particular subcase)
Let's assume this:
In this case,
ComponentHighestPrecedence
will be always selected, as expected. If we remove it, however,ComponentLowestPrecedence
will be always selected, due to how https://github.com/spring-projects/spring-framework/blob/main/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java#L1787 is currently implemented.Although that's probably by design, there is a much bigger issue here: there is no way to say that a component should have priority lower than or equal to "no explicit priority".
LOWEST_PRECEDENCE
is thus a misnomer in this case, it's "lower than any explicit but higher than all implicit".The use case when this is problematic is very simple: let's say we create a library that provides a bean that needs to be used only if the library consumer doesn't provide his own implementation. The workaround is to force the consumer to use
@Primary
, but that forces maintenance on the consumer. OTOH, if we would have a default reasonable priority value for components with no@Priority
, we could just declare@Priority(DEFAULT_PRECEDENCE - 1)
on the library component, and be done with it.Changing this is obviously a breaking change, but maybe there is some reasonable way to provide this as a configuration option somewhere, that would make e.g.
getPriority
return0
(or even better, someDEFAULT_PRECEDENCE
value) instead ofnull
for the beans that have no priority given explicitly?The text was updated successfully, but these errors were encountered: