@ConditionalOnMissingBean is considred in a Non-Boot Spring-App master
authorKai Moritz <kai@juplo.de>
Mon, 18 Sep 2023 19:14:19 +0000 (21:14 +0200)
committerKai Moritz <kai@juplo.de>
Mon, 18 Sep 2023 21:03:24 +0000 (23:03 +0200)
boot/src/main/java/de/juplo/boot/conditional/boot/Application.java [new file with mode: 0644]
lib/src/main/java/de/juplo/boot/conditional/lib/BeanA.java [new file with mode: 0644]
lib/src/main/java/de/juplo/boot/conditional/lib/DefaultConfig.java [new file with mode: 0644]
lib/src/main/java/de/juplo/boot/conditional/lib/SpecializedConfig.java [new file with mode: 0644]
spring/src/main/java/de/juplo/boot/conditional/spring/Application.java [new file with mode: 0644]

diff --git a/boot/src/main/java/de/juplo/boot/conditional/boot/Application.java b/boot/src/main/java/de/juplo/boot/conditional/boot/Application.java
new file mode 100644 (file)
index 0000000..3935ddd
--- /dev/null
@@ -0,0 +1,21 @@
+package de.juplo.boot.conditional.boot;
+
+import de.juplo.boot.conditional.lib.DefaultConfig;
+import de.juplo.boot.conditional.lib.SpecializedConfig;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Import;
+
+
+@SpringBootApplication
+@Import({
+    SpecializedConfig.class,
+    DefaultConfig.class,
+})
+public class Application
+{
+  public static void main(String[] args)
+  {
+    SpringApplication.run(Application.class, args);
+  }
+}
diff --git a/lib/src/main/java/de/juplo/boot/conditional/lib/BeanA.java b/lib/src/main/java/de/juplo/boot/conditional/lib/BeanA.java
new file mode 100644 (file)
index 0000000..cb2e01f
--- /dev/null
@@ -0,0 +1,18 @@
+package de.juplo.boot.conditional.lib;
+
+import lombok.Getter;
+import lombok.extern.slf4j.Slf4j;
+
+
+@Slf4j
+public final class BeanA
+{
+  @Getter
+  private final String name;
+
+  public BeanA(String name)
+  {
+    this.name = name;
+    log.info("BeanA was instanciated: {}", name);
+  }
+}
diff --git a/lib/src/main/java/de/juplo/boot/conditional/lib/DefaultConfig.java b/lib/src/main/java/de/juplo/boot/conditional/lib/DefaultConfig.java
new file mode 100644 (file)
index 0000000..27300e5
--- /dev/null
@@ -0,0 +1,17 @@
+package de.juplo.boot.conditional.lib;
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+
+@Configuration
+public class DefaultConfig
+{
+  @ConditionalOnMissingBean()
+  @Bean
+  public BeanA beanA()
+  {
+    return new BeanA("Default for BeanA");
+  }
+}
diff --git a/lib/src/main/java/de/juplo/boot/conditional/lib/SpecializedConfig.java b/lib/src/main/java/de/juplo/boot/conditional/lib/SpecializedConfig.java
new file mode 100644 (file)
index 0000000..f273288
--- /dev/null
@@ -0,0 +1,15 @@
+package de.juplo.boot.conditional.lib;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+
+@Configuration
+public class SpecializedConfig
+{
+  @Bean
+  public BeanA beanA()
+  {
+    return new BeanA("Specialized BeanA");
+  }
+}
diff --git a/spring/src/main/java/de/juplo/boot/conditional/spring/Application.java b/spring/src/main/java/de/juplo/boot/conditional/spring/Application.java
new file mode 100644 (file)
index 0000000..fa9b917
--- /dev/null
@@ -0,0 +1,23 @@
+package de.juplo.boot.conditional.spring;
+
+import de.juplo.boot.conditional.lib.DefaultConfig;
+import de.juplo.boot.conditional.lib.SpecializedConfig;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+
+
+@Configuration
+@Import({
+    SpecializedConfig.class,
+    DefaultConfig.class,
+})
+public class Application
+{
+  public static void main(String[] args)
+  {
+    ApplicationContext context =
+        new AnnotationConfigApplicationContext(Application.class);
+  }
+}