From e22620bdd12d3a3b79caed596a7e86e424fa78ed Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Mon, 18 Sep 2023 21:14:19 +0200 Subject: [PATCH] @ConditionalOnMissingBean is considred in a Non-Boot Spring-App --- .../boot/conditional/boot/Application.java | 21 +++++++++++++++++ .../de/juplo/boot/conditional/lib/BeanA.java | 18 +++++++++++++++ .../boot/conditional/lib/DefaultConfig.java | 17 ++++++++++++++ .../conditional/lib/SpecializedConfig.java | 15 ++++++++++++ .../boot/conditional/spring/Application.java | 23 +++++++++++++++++++ 5 files changed, 94 insertions(+) create mode 100644 boot/src/main/java/de/juplo/boot/conditional/boot/Application.java create mode 100644 lib/src/main/java/de/juplo/boot/conditional/lib/BeanA.java create mode 100644 lib/src/main/java/de/juplo/boot/conditional/lib/DefaultConfig.java create mode 100644 lib/src/main/java/de/juplo/boot/conditional/lib/SpecializedConfig.java create mode 100644 spring/src/main/java/de/juplo/boot/conditional/spring/Application.java 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 index 0000000..3935ddd --- /dev/null +++ b/boot/src/main/java/de/juplo/boot/conditional/boot/Application.java @@ -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 index 0000000..cb2e01f --- /dev/null +++ b/lib/src/main/java/de/juplo/boot/conditional/lib/BeanA.java @@ -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 index 0000000..27300e5 --- /dev/null +++ b/lib/src/main/java/de/juplo/boot/conditional/lib/DefaultConfig.java @@ -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 index 0000000..f273288 --- /dev/null +++ b/lib/src/main/java/de/juplo/boot/conditional/lib/SpecializedConfig.java @@ -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 index 0000000..fa9b917 --- /dev/null +++ b/spring/src/main/java/de/juplo/boot/conditional/spring/Application.java @@ -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); + } +} -- 2.20.1