#35 springboot: importSelector
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
package hello.selector;
|
||||||
|
|
||||||
|
public class HelloBean {
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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"};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user