From: Kai Moritz Date: Mon, 18 Sep 2023 19:14:19 +0000 (+0200) Subject: @ConditionalOnMissingBean is considred in a Non-Boot Spring-App X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;ds=sidebyside;p=demos%2Fspring%2Fconditional @ConditionalOnMissingBean is considred in a Non-Boot Spring-App --- 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); + } +}