-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[MNG-8764] Sort injected lists by @Priority annotation #2425
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
Conversation
When injecting List<T> dependencies, the DI container now sorts the bindings by their @priority annotation value in descending order (highest priority first) to ensure deterministic ordering. This change ensures that components with higher priority values appear first in injected lists, providing predictable behavior for dependency injection scenarios where order matters. - Modified InjectorImpl.doGetCompiledBinding() to sort bindings by priority before creating supplier lists - Added comprehensive tests for priority-based list ordering - Includes tests for mixed priorities and default priority handling
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well done.
might consider IoC (Dependency Inversion Principle), as part of SOLID principles afterward.
| List<Supplier<Object>> list = | ||
| sortedBindings.stream().map(this::compile).collect(Collectors.toList()); | ||
| //noinspection unchecked | ||
| return () -> (Q) list(list, Supplier::get); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| sortedBindings.sort(comparing.reversed()); | ||
|
|
||
| List<Supplier<Object>> list = | ||
| sortedBindings.stream().map(this::compile).collect(Collectors.toList()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
List<Binding<Object>> sortedBindings = new ArrayList<>(res2);
sortedBindings.sort(Comparator.reverseOrder());
//noinspection unchecked
return () -> (Q) list(sortedBindings.stream().map(this::compile).collect(Collectors.toList()), Supplier::get); @Override
public int compareTo(Binding<Object> objectBinding) {
return Integer.compare(this.priority, objectBinding.priority);
}
This is an IoC container, I wouldn't want to use IoC to bootstrap it ;-) |
why not? IoC is a pattern generally valid and possible to apply in this case as well. Current implementation seems to be entangled with feature envy, as the sorting logic is an random impl. detail, not of interest to the actual Its task it to apply to sorting not to actually do it. Instead of delegating the topic to the real one, its done by its consumer, making the code impossible to reuse. |
Did you read the full sentence ? Maven DI is an IoC container. It should not depend on another IoC container to actually be created. The other parts of Maven do rely on Maven DI, or Sisu containers for IoC. But specific bits, and especially the |
|
Its just about moving the external Imho this has nothing to do with an actual container, or what so ever. Im not injecting anything its just a concept of inverting the logic. The topic i want to talk about does not know about container or whatever. Its about giving SOC, avoiding feature envy leading to coupling instead of cohesion. we can merge this and evolve design afterward, its no problem. I will try if my theory is working. |
| // Sort bindings by priority (highest first) for deterministic ordering | ||
| List<Binding<Object>> sortedBindings = new ArrayList<>(res2); | ||
| Comparator<Binding<Object>> comparing = Comparator.comparing(Binding::getPriority); | ||
| sortedBindings.sort(comparing.reversed()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| sortedBindings.sort(comparing.reversed()); | |
| sortedBindings.sort(Binding.getPriorityComparator()); |
could shift feature envy in dedicated domain Binding.
public Comparator<Binding<Object>> getPriorityComparator() {
return Comparator.comparing(Binding::getPriority);
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, it's plain old java, but it's unrelated to IOC, which is generally about injecting dependencies in components rather than letting them create their own dependencies.
Yes, please raise a JIRA issue and a PR. The |
yes, i mean the concept behind it, not some random impl. detail. |
merci. |
When injecting List<T> dependencies, the DI container now sorts the bindings by their @priority annotation value in descending order (highest priority first) to ensure deterministic ordering. This change ensures that components with higher priority values appear first in injected lists, providing predictable behavior for dependency injection scenarios where order matters. - Modified InjectorImpl.doGetCompiledBinding() to sort bindings by priority before creating supplier lists - Added comprehensive tests for priority-based list ordering - Includes tests for mixed priorities and default priority handling
|
Resolve #9344 |


When injecting List dependencies, the DI container now sorts the
bindings by their @priority annotation value in descending order
(highest priority first) to ensure deterministic ordering.
This change ensures that components with higher priority values
appear first in injected lists, providing predictable behavior
for dependency injection scenarios where order matters.
by priority before creating supplier lists
JIRA issue: MNG-8764