Skip to content
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

@Order on nested @Configuration bug #30750

Closed
mironbalcerzak opened this issue Jun 26, 2023 · 3 comments
Closed

@Order on nested @Configuration bug #30750

mironbalcerzak opened this issue Jun 26, 2023 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid

Comments

@mironbalcerzak
Copy link

mironbalcerzak commented Jun 26, 2023

Affects: 5.2.24.RELEASE, 5.3.28, 6.0.10

Summary:

@Order on nested @Configuration classes is not respected in certain scenarios.

Once static is removed, bean loading occurs as expected (and that's against what is documented for @Configuration annotation).

Here are diagrams and related code:

https://github.com/mironbalcerzak/poc-spring-config-ordering

Here's SO: https://stackoverflow.com/questions/76552375/order-ing-of-configuration-for-bean-overriding

Have a great day =)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 26, 2023
@bclozel bclozel added the in: core Issues in core modules (aop, beans, core, context, expression) label Jul 17, 2023
@mironbalcerzak
Copy link
Author

mironbalcerzak commented Aug 17, 2023

@bclozel any feedback?
CC: @philwebb @wilkinsona

@jhoeller jhoeller self-assigned this Dec 29, 2023
@jhoeller
Copy link
Contributor

Revisiting the ordering affect for configuration classes along with documenting the current behavior in #30177.

Generally speaking, ordering among configuration classes is not meant to be a hard declaration - in particular not for bean overrides by name in those configuration classes -, just a soft ordering between otherwise arbitrary or name-based registration order.

@jhoeller jhoeller added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 29, 2023
@jhoeller
Copy link
Contributor

The effect that you are seeing comes from the classpath scan finding the nested classes directly rather than through their containing class. As a consequence, it processes them in the order that it found them in the classpath. Through declaring those nested classes as non-static, classpath scanning does not consider them as independent anymore, so they will actually be processed through their containing class then - with the order for nested classes respected there. You can achieve the same effect by removing the @Configuration stereotype from the nested classes (e.g. A1, A2) so that classpath scanning does not identify them anymore; this works with static classes as well since they will only be found through their containing class then.

As a recommendation, if you rely on ordering for any such structure, do not use classpath scanning at all for that part. And even more generally, try to not rely on ordering at all. Bean definition overriding can be surprising even in straightforward scenarios, and such nesting with ordering for bean definition override purposes is bound to cause maintenance headaches.

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) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

4 participants