Skip to content

Add public method to get bean order #34712

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
mhalbritter opened this issue Apr 3, 2025 · 4 comments
Open

Add public method to get bean order #34712

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

Comments

@mhalbritter
Copy link
Contributor

mhalbritter commented Apr 3, 2025

Hello,

in Boot we have the use-case to get the order of a bean. We have the bean instance, and also the bean name.

We tried:

return new AnnotationAwareOrderComparator() {
	@Override
	public int getOrder(Object obj) {
		return super.getOrder(obj);
	}
}.getOrder(bean);

but this only looks at the bean itself, not the factory method.

Combined with this:

Order orderAnnotation = beanFactory.findAnnotationOnBean(beanName, Order.class);
if (orderAnnotation != null) {
	return orderAnnotation.value();
}

it also supports @Order on the bean method, but it's missing @Priority (is that supported on bean methods?) and, looking at org.springframework.beans.factory.support.DefaultListableBeanFactory.FactoryAwareOrderSourceProvider there's also a ORDER attribute on the bean definition.

There's also org.springframework.core.annotation.OrderUtils but this only looks at annotations, not at implements Ordered.

Would it be possible to provide a way to reliably get the order of the bean?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 3, 2025
@mhalbritter mhalbritter changed the title Method to get bean order Add public method to get bean order Apr 3, 2025
@jhoeller jhoeller added the in: core Issues in core modules (aop, beans, core, context, expression) label Apr 3, 2025
@jhoeller jhoeller self-assigned this Apr 3, 2025
@jhoeller
Copy link
Contributor

jhoeller commented Apr 3, 2025

Ordering is semantically rather involved, not only in terms of potential order value sources, but also considering PriorityOrdered versus Ordered which effectively indicates two separate order value ranges in a mixed sort attempt.

So while we could add a int getOrder(beanName) method to DefaultListableBeanFactory, it would come with the limitation of not differentiating between priority order and regular order. Also, it would just work against singletons, otherwise we'd have to pass in the current bean instance as well. Defining the semantics for such a public method outside of a concrete sort attempt is not obvious.

What's the use case in Boot to independently introspect the order value?

@mhalbritter
Copy link
Contributor Author

Thanks for taking a look, @jhoeller.

In org.springframework.boot.web.servlet.ServletContextInitializerBeans, we're essentially finding all beans of type Servlet and Filter (see addAdaptableBeans) and we're creating ServletRegistrationBean and FilterRegistrationBean beans for them. Those RegistrationBean beans should have the same order as the (Servlet and Filter) bean for which they have been created.

Also, it would just work against singletons, otherwise we'd have to pass in the current bean instance as well.

This won't be a problem in this case, as we have both the bean name and the instance.

@jhoeller
Copy link
Contributor

jhoeller commented Apr 3, 2025

Ah, so you are trying to carry the order over from a target bean to the current bean, locally exposing it through your own Ordered.getOrder() implementation? For certain kinds of components, we could probably expect those target beans to implement Ordered themselves for such an arrangement. However, for freely definable target beans, it's indeed a problem of taking all order sources into account for your Ordered.getOrder() implementation at the front then.

@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 3, 2025
@jhoeller jhoeller added this to the 7.0.x milestone Apr 3, 2025
@mhalbritter
Copy link
Contributor Author

Ah, so you are trying to carry the order over from a target bean to the current bean, locally exposing it through your own Ordered.getOrder() implementation?

Yes, correct.

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

3 participants