Allow filtering of modules before initialization

Alternative for #111
This commit is contained in:
Niklas Herder
2023-06-19 16:13:06 +02:00
committed by Dave Syer
parent b4048a24c0
commit b202e964a2
4 changed files with 70 additions and 1 deletions

View File

@@ -75,6 +75,14 @@ public class EnableGuiceModulesTests {
context.close();
}
@Test
public void moduleBeanFiltersOutModules() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
FilteringModuleBeanConfig.class);
assertThat(context.getBean(Foo.class)).isNotNull();
context.close();
}
@Test
public void testInjectorCreationDoesNotCauseCircularDependencyError() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MySpringConfig.class);
@@ -151,6 +159,32 @@ public class EnableGuiceModulesTests {
}
@Configuration(proxyBeanMethods = false)
@EnableGuiceModules
protected static class FilteringModuleBeanConfig {
@Bean
public static MyModule2 module2() {
return new MyModule2();
}
@Bean
public static MyModule module() {
return new MyModule();
}
@Bean
public Foo service(Service service) {
return new Foo(service);
}
@Bean
ModuleFilter moduleFilter() {
return module -> !(module instanceof MyModule2);
}
}
protected static class MyModule extends AbstractModule {
@Override
@@ -160,6 +194,15 @@ public class EnableGuiceModulesTests {
}
protected static class MyModule2 extends AbstractModule {
@Override
protected void configure() {
throw new RuntimeException("This should not be called when filtered out!");
}
}
public static class SpringProvidedBean {
public SpringProvidedBean(GuiceProvidedBean guiceProvidedBean) {