2

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 apache artemis to let spring boots 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.

SpaceTrucker
  • 13,377
  • 6
  • 60
  • 99

1 Answers1

0

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
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • This won't help because `ConditionalOnBean` is evaluated during bean registration and hence before bean creation. I also actually have an explicit `@DependsOn` because the mentioned bean methods with `@ConditionOnBean(ConnectionFactory.class)` also use the connection factory as a parameter. But if I start the application with jms disabled I only want these beans not to be created instead of the application failing to start because of the missing connection factory. – SpaceTrucker Aug 31 '23 at 11:33
  • Than you can create different profiles and for a particular profile have some beans not registered. I think the profiles is your option – Michael Gantman Aug 31 '23 at 11:53