From 70239ffc513e873943b568dae58414daecfaf9eb Mon Sep 17 00:00:00 2001 From: haerong22 Date: Wed, 15 Mar 2023 03:17:30 +0900 Subject: [PATCH] #35 springboot: importSelector --- .../test/java/hello/selector/HelloBean.java | 4 ++ .../test/java/hello/selector/HelloConfig.java | 13 ++++++ .../hello/selector/HelloImportSelector.java | 11 +++++ .../hello/selector/ImportSelectorTest.java | 40 +++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 springboot/autoconfig/src/test/java/hello/selector/HelloBean.java create mode 100644 springboot/autoconfig/src/test/java/hello/selector/HelloConfig.java create mode 100644 springboot/autoconfig/src/test/java/hello/selector/HelloImportSelector.java create mode 100644 springboot/autoconfig/src/test/java/hello/selector/ImportSelectorTest.java diff --git a/springboot/autoconfig/src/test/java/hello/selector/HelloBean.java b/springboot/autoconfig/src/test/java/hello/selector/HelloBean.java new file mode 100644 index 00000000..3f28e516 --- /dev/null +++ b/springboot/autoconfig/src/test/java/hello/selector/HelloBean.java @@ -0,0 +1,4 @@ +package hello.selector; + +public class HelloBean { +} diff --git a/springboot/autoconfig/src/test/java/hello/selector/HelloConfig.java b/springboot/autoconfig/src/test/java/hello/selector/HelloConfig.java new file mode 100644 index 00000000..0dc5780f --- /dev/null +++ b/springboot/autoconfig/src/test/java/hello/selector/HelloConfig.java @@ -0,0 +1,13 @@ +package hello.selector; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class HelloConfig { + + @Bean + public HelloBean helloBean() { + return new HelloBean(); + } +} diff --git a/springboot/autoconfig/src/test/java/hello/selector/HelloImportSelector.java b/springboot/autoconfig/src/test/java/hello/selector/HelloImportSelector.java new file mode 100644 index 00000000..3416217c --- /dev/null +++ b/springboot/autoconfig/src/test/java/hello/selector/HelloImportSelector.java @@ -0,0 +1,11 @@ +package hello.selector; + +import org.springframework.context.annotation.ImportSelector; +import org.springframework.core.type.AnnotationMetadata; + +public class HelloImportSelector implements ImportSelector { + @Override + public String[] selectImports(AnnotationMetadata importingClassMetadata) { + return new String[]{"hello.selector.HelloConfig"}; + } +} diff --git a/springboot/autoconfig/src/test/java/hello/selector/ImportSelectorTest.java b/springboot/autoconfig/src/test/java/hello/selector/ImportSelectorTest.java new file mode 100644 index 00000000..d5a24fb0 --- /dev/null +++ b/springboot/autoconfig/src/test/java/hello/selector/ImportSelectorTest.java @@ -0,0 +1,40 @@ +package hello.selector; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +import static org.assertj.core.api.Assertions.*; + +public class ImportSelectorTest { + + @Test + void staticConfig() { + AnnotationConfigApplicationContext appContext = + new AnnotationConfigApplicationContext(HelloConfig.class); + HelloBean bean = appContext.getBean(HelloBean.class); + assertThat(bean).isNotNull(); + } + + @Test + void selectorConfig() { + AnnotationConfigApplicationContext appContext = + new AnnotationConfigApplicationContext(SelectorConfig.class); + HelloBean bean = appContext.getBean(HelloBean.class); + assertThat(bean).isNotNull(); + } + + @Configuration + @Import(HelloConfig.class) + public static class StaticConfig { + + } + + @Configuration + @Import(HelloImportSelector.class) + public static class SelectorConfig { + + } +}