What determines spring boots bean registration order for auto configurations?

190 views Asked by At

I have a multi-subproject Spring Boot 3.0 application built by gradle. In modules A and B I have beans with the annotations

@Bean
@ConditionalOnBean(jakarta.jms.ConnectionFactory.class)

declared.

The actual app subproject containing the runnable boot application depends on both of these modules. Also module B depends on A. And app also has a runtimeOnly dependency on ActiveMQ Artemis to let Spring Boot's ArtemisAutoConfiguration do the configuration of the JMS connection factory via ArtemisAutoConfiguration.

However, when I look at the condition evaluation report after the application started I see that the condition did not match for the bean defined in subproject A but it did match for the bean defined in subproject B. Since ConditionalOnBean is evaluated during bean registration phase I suppose the bean definition for artemis is registered only after the bean in subproject A is registered but before the one in subproject B.

Can anyone explain what determines this order?

I already had a look at BOOT-INF/classpath.idx in the spring boot jar. But this shows A and B before any artemis and spring-boot-autoconfiguration entries. So I suppose it's not that.

NOTE: yes, using @AutoConfigureAfter(ArtemisAutoConfiguration.class) on the auto configuration of subproject A fixes this. I'm just curious to know why this was actually necessary to apply.

1

There are 1 answers

2
Michael Gantman On

I am not familiar with @ConditionalOnBean, but you also can control creation beans order with @Order and @DependsOn. I used @DependsOn and it works well. Here are two articles that may help:

  1. @Order in Spring,
  2. Controlling Bean Creation Order with @DependsOn Annotation