create injector before embedded server initialization, after other BeanPostProcessors

This commit is contained in:
Taylor Wicksell
2018-03-01 11:46:05 -06:00
committed by Taylor Wicksell
parent 5e3603c29d
commit 1680610faf
4 changed files with 69 additions and 43 deletions

View File

@@ -7,9 +7,14 @@ import javax.inject.Inject;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.guice.BeanPostProcessorTests.GuiceBeanThatWantsPostProcessedBean;
import org.springframework.guice.BeanPostProcessorTests.GuiceBeanThatWantsSpringBean;
import org.springframework.guice.BeanPostProcessorTests.PostProcessedBean;
@@ -69,22 +74,42 @@ public class BeanPostProcessorTests {
@EnableGuiceModules
@Configuration
class BeanPostProcessorTestConfig {
public static class PostProcessorRegistrar implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
BeanDefinitionBuilder bean = BeanDefinitionBuilder.genericBeanDefinition(TestBeanPostProcessor.class);
registry.registerBeanDefinition("postProcessor", bean.getBeanDefinition());
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {}
}
public static class TestBeanPostProcessor implements BeanPostProcessor, Ordered {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof PostProcessedBean) {
((PostProcessedBean)bean).postProcessed = true;
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public int getOrder() {
return 0;
}
}
@Bean
public BeanPostProcessor postProcessor() {
return new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof PostProcessedBean) {
((PostProcessedBean)bean).postProcessed = true;
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
};
public PostProcessorRegistrar postProcessorRegistrar() {
return new PostProcessorRegistrar();
}
@Bean

View File

@@ -1,12 +1,11 @@
package org.springframework.guice;
import com.google.inject.AbstractModule;
import com.google.inject.CreationException;
import com.google.inject.Module;
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;
@@ -32,7 +31,7 @@ public class BindingDeduplicationTests {
context.close();
}
@Test(expected = CreationException.class)
@Test(expected = BeanCreationException.class)
public void verifyDuplicateBindingErrorWhenDedupeNotEnabled() {
System.setProperty("spring.guice.dedup", "false");
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(

View File

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