Fixed detection of generic types and qualifier annotations on scoped-proxy factory methods
Issue: SPR-11116
This commit is contained in:
@@ -158,8 +158,43 @@ public class ConfigurationClassPostProcessorTests {
|
||||
pp.postProcessBeanFactory(beanFactory);
|
||||
|
||||
RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
|
||||
assertSame(beanFactory.getBean("stringRepo"), bean.stringRepository);
|
||||
assertSame(beanFactory.getBean("integerRepo"), bean.integerRepository);
|
||||
assertEquals("Repository<String>", bean.stringRepository.toString());
|
||||
assertEquals("Repository<Integer>", bean.integerRepository.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericsBasedInjectionWithScoped() {
|
||||
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
|
||||
bpp.setBeanFactory(beanFactory);
|
||||
beanFactory.addBeanPostProcessor(bpp);
|
||||
RootBeanDefinition bd = new RootBeanDefinition(RepositoryInjectionBean.class);
|
||||
bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
|
||||
beanFactory.registerBeanDefinition("annotatedBean", bd);
|
||||
beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ScopedRepositoryConfiguration.class));
|
||||
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
|
||||
pp.postProcessBeanFactory(beanFactory);
|
||||
|
||||
RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
|
||||
assertEquals("Repository<String>", bean.stringRepository.toString());
|
||||
assertEquals("Repository<Integer>", bean.integerRepository.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericsBasedInjectionWithScopedProxy() {
|
||||
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
|
||||
bpp.setBeanFactory(beanFactory);
|
||||
beanFactory.addBeanPostProcessor(bpp);
|
||||
RootBeanDefinition bd = new RootBeanDefinition(RepositoryInjectionBean.class);
|
||||
bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
|
||||
beanFactory.registerBeanDefinition("annotatedBean", bd);
|
||||
beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ScopedProxyRepositoryConfiguration.class));
|
||||
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
|
||||
pp.postProcessBeanFactory(beanFactory);
|
||||
beanFactory.freezeConfiguration();
|
||||
|
||||
RepositoryInjectionBean bean = (RepositoryInjectionBean) beanFactory.getBean("annotatedBean");
|
||||
assertEquals("Repository<String>", bean.stringRepository.toString());
|
||||
assertEquals("Repository<Integer>", bean.integerRepository.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -295,12 +330,72 @@ public class ConfigurationClassPostProcessorTests {
|
||||
|
||||
@Bean
|
||||
public Repository<String> stringRepo() {
|
||||
return new Repository<String>();
|
||||
return new Repository<String>() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Repository<String>";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Repository<Integer> integerRepo() {
|
||||
return new Repository<Integer>();
|
||||
return new Repository<Integer>() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Repository<Integer>";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
public static class ScopedRepositoryConfiguration {
|
||||
|
||||
@Bean @Scope("prototype")
|
||||
public Repository<String> stringRepo() {
|
||||
return new Repository<String>() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Repository<String>";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean @Scope("prototype")
|
||||
public Repository<Integer> integerRepo() {
|
||||
return new Repository<Integer>() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Repository<Integer>";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
public static class ScopedProxyRepositoryConfiguration {
|
||||
|
||||
@Bean @Scope(value="prototype", proxyMode=ScopedProxyMode.TARGET_CLASS)
|
||||
public Repository<String> stringRepo() {
|
||||
return new Repository<String>() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Repository<String>";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean @Scope(value="prototype", proxyMode=ScopedProxyMode.TARGET_CLASS)
|
||||
public Repository<Integer> integerRepo() {
|
||||
return new Repository<Integer>() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Repository<Integer>";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,16 +21,17 @@ import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.tests.sample.beans.NestedTestBean;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.context.annotation.ScopedProxyMode;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.tests.sample.beans.NestedTestBean;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
@@ -53,6 +54,24 @@ public class BeanMethodQualificationTests {
|
||||
assertThat(pojo.testBean.getName(), equalTo("interesting"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScoped() {
|
||||
AnnotationConfigApplicationContext ctx =
|
||||
new AnnotationConfigApplicationContext(ScopedConfig.class, StandardPojo.class);
|
||||
assertFalse(ctx.getBeanFactory().containsSingleton("testBean1"));
|
||||
StandardPojo pojo = ctx.getBean(StandardPojo.class);
|
||||
assertThat(pojo.testBean.getName(), equalTo("interesting"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScopedProxy() {
|
||||
AnnotationConfigApplicationContext ctx =
|
||||
new AnnotationConfigApplicationContext(ScopedProxyConfig.class, StandardPojo.class);
|
||||
assertTrue(ctx.getBeanFactory().containsSingleton("testBean1")); // a shared scoped proxy
|
||||
StandardPojo pojo = ctx.getBean(StandardPojo.class);
|
||||
assertThat(pojo.testBean.getName(), equalTo("interesting"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustom() {
|
||||
AnnotationConfigApplicationContext ctx =
|
||||
@@ -75,7 +94,7 @@ public class BeanMethodQualificationTests {
|
||||
@Configuration
|
||||
static class StandardConfig {
|
||||
|
||||
@Bean @Lazy @Qualifier("interesting")
|
||||
@Bean @Qualifier("interesting") @Lazy
|
||||
public TestBean testBean1() {
|
||||
return new TestBean("interesting");
|
||||
}
|
||||
@@ -86,6 +105,34 @@ public class BeanMethodQualificationTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class ScopedConfig {
|
||||
|
||||
@Bean @Qualifier("interesting") @Scope("prototype")
|
||||
public TestBean testBean1() {
|
||||
return new TestBean("interesting");
|
||||
}
|
||||
|
||||
@Bean @Qualifier("boring") @Scope("prototype")
|
||||
public TestBean testBean2() {
|
||||
return new TestBean("boring");
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class ScopedProxyConfig {
|
||||
|
||||
@Bean @Qualifier("interesting") @Scope(value="prototype", proxyMode=ScopedProxyMode.TARGET_CLASS)
|
||||
public TestBean testBean1() {
|
||||
return new TestBean("interesting");
|
||||
}
|
||||
|
||||
@Bean @Qualifier("boring") @Scope(value="prototype", proxyMode=ScopedProxyMode.TARGET_CLASS)
|
||||
public TestBean testBean2() {
|
||||
return new TestBean("boring");
|
||||
}
|
||||
}
|
||||
|
||||
@Component @Lazy
|
||||
static class StandardPojo {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user