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 =)

Comment From: mironbalcerzak

@bclozel any feedback? CC: @philwebb @wilkinsona

Comment From: jhoeller

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.

Comment From: jhoeller

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.