Skip to content

Order metadata to enforce appearance at the end of a sorted collection #34670

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

Open
mipo256 opened this issue Mar 28, 2025 · 3 comments
Open

Order metadata to enforce appearance at the end of a sorted collection #34670

mipo256 opened this issue Mar 28, 2025 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@mipo256
Copy link

mipo256 commented Mar 28, 2025

Overview

I kindly want to ask to resume the conversation on this topic.

It seems, that people have to deal with this problem.

What is desired, is that assume we have 3 beans: A, B and NoOp. And I assemble them into a List somewhere via:

@Autowired
private List<CommonInterface> list;

And what I want is the following:

for (var impl : list) {
    if (impl.supports(o)) {
        impl.doWork(o);
    }
}

The NoOp implementation is required to be the last, since it is fallback. But I really do not care about the order of other beans in the List. What I want is to ensure, that the NoOp bean is the last one in the List.

The problem is that simply adding @Order with the lowest priority to the NoOp will not work.

I can, of course, define the @Order with higher priority for each bean except for NoOp, but this requires that every time a new implementation of CommonInterface being added, the developer must not forget about adding the corresponding order marker as well. Otherwise, the NoOp may not be the last, which can lead and probably would lead to bugs.

The deeper problem is that if I deal with already compiled code, and I cannot physically add any @Order/Ordered to the compiled implementations, but I need to add my own to the end of the list.

I hope we can collaboratively find a solution. Thank you.

Related Issues

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 28, 2025
@quaff
Copy link
Contributor

quaff commented Mar 31, 2025

You can annotate NoOp with @Fallback, it's new feature since 6.2.

Currently @Fallback doesn't affect the order, but I think it should.

@jhoeller jhoeller added the in: core Issues in core modules (aop, beans, core, context, expression) label Apr 2, 2025
@jhoeller
Copy link
Contributor

jhoeller commented Apr 2, 2025

Design-wise, @Primary/@Fallback and @Order are rather orthogonal concerns. The former annotations are used in the dependency resolution algorithm for selecting a single match among multiple candidates, whereas the latter - as part of our ordering/sorting arrangement - is used for multi-element exposure in collections, arrays, and streams.

To me, your request sounds like a variant of the PriorityOrdered interface next to Ordered, just on the opposite end of the spectrum. We could introduce such an opposite marker interface for a whole range of values sorted behind regular Ordered, just like PriorityOrdered provides such a value range before regular Ordered.

However, even PriorityOrdered is not exposed for @Order annotation usage. So if we had an XxxOrdered interface for the other end of the spectrum, your NoOp bean would have to implement that interface directly. I'm not keen on exposing those categories of exposure on the @Order annotation through an enum attribute or the like; ideally, those special categories should remain interface-based.

@jhoeller jhoeller self-assigned this Apr 2, 2025
@jhoeller jhoeller added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 2, 2025
@jhoeller jhoeller changed the title Beans without order metadata still appear at the end of collection Order metadata to enforce appearance at the end of a sorted collection Apr 2, 2025
@jhoeller jhoeller added this to the 7.0.x milestone Apr 2, 2025
@sbrannen
Copy link
Member

sbrannen commented Apr 8, 2025

Let's keep in mind that this is related to the following, which we already have in the general backlog.

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) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants