I had a problem that a @Service I created with @ConditionOnBean had an error that this very bean is required to created it and is missing.

I tried to created simplified example. I created an example with 3 services with @ConditionalOnBean: Service1, Service2 and Service3.

Service2 is @ConditionalOnBean on a non existing bean. Service1 and Service3 are ConditionalOnBean on Service2.

The beans log their creation in the constructor.

I expected non of the bean to be created but the results were inconsistent. When I ran from within IntelliJ (by right clicking Main class) Service1 was created. When I ran from the command line (with java -jar) no service was created (which is the expected behavior).

I have the example code here: https://github.com/selalerercapitolis/two_conditional_on_bean_bug

The output when running from IntelliJ (see the Service1 created message):

/usr/lib/jvm/java-17-openjdk-amd64/bin/java -javaagent:/snap/intellij-idea-community/357/lib/idea_rt.jar=36245:/snap/intellij-idea-community/357/bin -Dfile.encoding=UTF-8 -classpath /home/sela/code/two_conditional_on_bean_bug/target/classes:/home/sela/.m2/repository/org/springframework/boot/spring-boot-starter-web/2.6.6/spring-boot-starter-web-2.6.6.jar:/home/sela/.m2/repository/org/springframework/boot/spring-boot-starter/2.6.6/spring-boot-starter-2.6.6.jar:/home/sela/.m2/repository/org/springframework/boot/spring-boot/2.6.6/spring-boot-2.6.6.jar:/home/sela/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.6.6/spring-boot-autoconfigure-2.6.6.jar:/home/sela/.m2/repository/org/springframework/boot/spring-boot-starter-logging/2.6.6/spring-boot-starter-logging-2.6.6.jar:/home/sela/.m2/repository/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar:/home/sela/.m2/repository/ch/qos/logback/logback-core/1.2.11/logback-core-1.2.11.jar:/home/sela/.m2/repository/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar:/home/sela/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.17.2/log4j-to-slf4j-2.17.2.jar:/home/sela/.m2/repository/org/apache/logging/log4j/log4j-api/2.17.2/log4j-api-2.17.2.jar:/home/sela/.m2/repository/org/slf4j/jul-to-slf4j/1.7.36/jul-to-slf4j-1.7.36.jar:/home/sela/.m2/repository/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar:/home/sela/.m2/repository/org/springframework/spring-core/5.3.18/spring-core-5.3.18.jar:/home/sela/.m2/repository/org/springframework/spring-jcl/5.3.18/spring-jcl-5.3.18.jar:/home/sela/.m2/repository/org/yaml/snakeyaml/1.29/snakeyaml-1.29.jar:/home/sela/.m2/repository/org/springframework/boot/spring-boot-starter-json/2.6.6/spring-boot-starter-json-2.6.6.jar:/home/sela/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.13.2.2/jackson-databind-2.13.2.2.jar:/home/sela/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.13.2/jackson-annotations-2.13.2.jar:/home/sela/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.13.2/jackson-core-2.13.2.jar:/home/sela/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.13.2/jackson-datatype-jdk8-2.13.2.jar:/home/sela/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.13.2/jackson-datatype-jsr310-2.13.2.jar:/home/sela/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.13.2/jackson-module-parameter-names-2.13.2.jar:/home/sela/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/2.6.6/spring-boot-starter-tomcat-2.6.6.jar:/home/sela/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.60/tomcat-embed-core-9.0.60.jar:/home/sela/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/9.0.60/tomcat-embed-el-9.0.60.jar:/home/sela/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.60/tomcat-embed-websocket-9.0.60.jar:/home/sela/.m2/repository/org/springframework/spring-web/5.3.18/spring-web-5.3.18.jar:/home/sela/.m2/repository/org/springframework/spring-beans/5.3.18/spring-beans-5.3.18.jar:/home/sela/.m2/repository/org/springframework/spring-webmvc/5.3.18/spring-webmvc-5.3.18.jar:/home/sela/.m2/repository/org/springframework/spring-aop/5.3.18/spring-aop-5.3.18.jar:/home/sela/.m2/repository/org/springframework/spring-context/5.3.18/spring-context-5.3.18.jar:/home/sela/.m2/repository/org/springframework/spring-expression/5.3.18/spring-expression-5.3.18.jar com.example.Main

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.6)

2022-04-26 21:00:28.062  INFO 202331 --- [           main] com.example.Main                         : Starting Main using Java 17.0.2 on IL-L-SELAL with PID 202331 (/home/sela/code/two_conditional_on_bean_bug/target/classes started by sela in /home/sela/code/two_conditional_on_bean_bug)
2022-04-26 21:00:28.065  INFO 202331 --- [           main] com.example.Main                         : No active profile set, falling back to 1 default profile: "default"
2022-04-26 21:00:28.635  INFO 202331 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-04-26 21:00:28.641  INFO 202331 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-04-26 21:00:28.641  INFO 202331 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.60]
2022-04-26 21:00:28.694  INFO 202331 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-04-26 21:00:28.694  INFO 202331 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 591 ms
2022-04-26 21:00:28.721  INFO 202331 --- [           main] com.example.Service1                     : Service1 created
2022-04-26 21:00:28.901  INFO 202331 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-04-26 21:00:28.911  INFO 202331 --- [           main] com.example.Main                         : Started Main in 1.152 seconds (JVM running for 1.387)

The output when running from the command line:

$ java -jar target/two_conditional_on_bean_bug-1.0.0.jar 

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.6)

2022-04-26 21:03:37.838  INFO 202766 --- [           main] com.example.Main                         : Starting Main v1.0.0 using Java 17.0.2 on IL-L-SELAL with PID 202766 (/home/sela/code/two_conditional_on_bean_bug/target/two_conditional_on_bean_bug-1.0.0.jar started by sela in /home/sela/code/two_conditional_on_bean_bug)
2022-04-26 21:03:37.840  INFO 202766 --- [           main] com.example.Main                         : No active profile set, falling back to 1 default profile: "default"
2022-04-26 21:03:38.440  INFO 202766 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-04-26 21:03:38.450  INFO 202766 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-04-26 21:03:38.450  INFO 202766 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.60]
2022-04-26 21:03:38.498  INFO 202766 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-04-26 21:03:38.499  INFO 202766 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 623 ms
2022-04-26 21:03:38.724  INFO 202766 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-04-26 21:03:38.730  INFO 202766 --- [           main] com.example.Main                         : Started Main in 1.183 seconds (JVM running for 1.474)

Comment From: wilkinsona

Please refer to the javadoc for @ConditionalOnBean where it says the following:

The condition can only match the bean definitions that have been processed by the application context so far and, as such, it is strongly recommended to use this condition on auto-configuration classes only. If a candidate bean may be created by another auto-configuration, make sure that the one using this condition runs after.

You are using @ConditionalOnBean on three @Service beans that are found and defined by component scanning. Component scanning makes no guarantees about ordering and it will vary depending on the behaviour of the class loader, file system ordering, etc.

You should define your service beans using @Bean methods in one or more auto-configuration classes that use @AutoConfigureBefore and @AutoConfigureAfter to guarantee the desired ordering.