Delay injector creation until after registerBeanPostProcessors() phase is complete

This commit is contained in:
Asi Bross
2021-08-27 10:13:38 -07:00
parent e54c544afa
commit 7434f407b6
5 changed files with 183 additions and 102 deletions

View File

@@ -87,7 +87,7 @@ class BeanPostProcessorTestConfig {
}
public static class TestBeanPostProcessor implements BeanPostProcessor, Ordered {
public static class TestBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof PostProcessedBean) {
@@ -100,11 +100,6 @@ class BeanPostProcessorTestConfig {
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public int getOrder() {
return 0;
}
}
@Bean

View File

@@ -1,12 +1,12 @@
package org.springframework.guice;
import com.google.inject.AbstractModule;
import com.google.inject.CreationException;
import com.google.inject.Module;
import com.google.inject.multibindings.OptionalBinder;
import org.junit.AfterClass;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -35,7 +35,7 @@ public class BindingDeduplicationTests {
context.close();
}
@Test(expected = BeanCreationException.class)
@Test(expected = CreationException.class)
public void verifyDuplicateBindingErrorWhenDedupeNotEnabled() {
System.setProperty("spring.guice.dedup", "false");
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(

View File

@@ -4,6 +4,7 @@ import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -31,7 +32,7 @@ public class InjectorFactoryTests {
context.close();
}
@Test(expected = BeanCreationException.class)
@Test(expected = ApplicationContextException.class)
public void testMultipleInjectorFactoriesThrowsApplicationContextException() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(InjectorFactoryConfig.class,
SecondInjectorFactoryConfig.class, ModulesConfig.class);

View File

@@ -19,6 +19,8 @@ import javax.inject.Named;
import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import org.junit.After;
import org.junit.Test;
@@ -75,6 +77,13 @@ public class EnableGuiceModulesTests {
context.close();
}
@Test
public void testInjectorCreationDoesNotCauseCircularDependencyError() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MySpringConfig.class);
assertNotNull(context.getBean(SpringProvidedBean.class));
context.close();
}
interface Service {
}
@@ -150,4 +159,50 @@ public class EnableGuiceModulesTests {
}
public static class SpringProvidedBean {
public SpringProvidedBean(GuiceProvidedBean guiceProvidedBean) {
}
}
public static class GuiceProvidedBean {
}
public static class GuiceService {
@Inject
public GuiceService(SpringProvidedBean springProvidedBean) {
}
}
public static class MyGuiceModule extends AbstractModule {
@Override
protected void configure() {
bind(GuiceService.class).asEagerSingleton();
}
@Provides
@Singleton
public GuiceProvidedBean guiceProvidedBean() {
return new GuiceProvidedBean();
}
}
@Configuration
@EnableGuiceModules
public static class MySpringConfig {
@Bean
public SpringProvidedBean baz(GuiceProvidedBean guiceProvidedBean) {
return new SpringProvidedBean(guiceProvidedBean);
}
@Bean
public MyGuiceModule bazModule() {
return new MyGuiceModule();
}
}
}