use of org.springframework.context.annotation4.FactoryMethodComponent in project spring-framework by spring-projects.
the class ClassPathFactoryBeanDefinitionScannerTests method testSingletonScopedFactoryMethod.
@Test
public void testSingletonScopedFactoryMethod() {
GenericApplicationContext context = new GenericApplicationContext();
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
context.getBeanFactory().registerScope("request", new SimpleMapScope());
scanner.scan(BASE_PACKAGE);
context.registerBeanDefinition("clientBean", new RootBeanDefinition(QualifiedClientBean.class));
context.refresh();
FactoryMethodComponent fmc = context.getBean("factoryMethodComponent", FactoryMethodComponent.class);
assertThat(fmc.getClass().getName().contains(ClassUtils.CGLIB_CLASS_SEPARATOR)).isFalse();
// 2
TestBean tb = (TestBean) context.getBean("publicInstance");
assertThat(tb.getName()).isEqualTo("publicInstance");
// 2
TestBean tb2 = (TestBean) context.getBean("publicInstance");
assertThat(tb2.getName()).isEqualTo("publicInstance");
assertThat(tb).isSameAs(tb2);
// 3
tb = (TestBean) context.getBean("protectedInstance");
assertThat(tb.getName()).isEqualTo("protectedInstance");
assertThat(context.getBean("protectedInstance")).isSameAs(tb);
assertThat(tb.getCountry()).isEqualTo("0");
// 3
tb2 = context.getBean("protectedInstance", TestBean.class);
assertThat(tb2.getName()).isEqualTo("protectedInstance");
assertThat(tb).isSameAs(tb2);
// 4
tb = context.getBean("privateInstance", TestBean.class);
assertThat(tb.getName()).isEqualTo("privateInstance");
assertThat(tb.getAge()).isEqualTo(1);
// 4
tb2 = context.getBean("privateInstance", TestBean.class);
assertThat(tb2.getAge()).isEqualTo(2);
assertThat(tb).isNotSameAs(tb2);
// 5
Object bean = context.getBean("requestScopedInstance");
assertThat(AopUtils.isCglibProxy(bean)).isTrue();
boolean condition = bean instanceof ScopedObject;
assertThat(condition).isTrue();
QualifiedClientBean clientBean = context.getBean("clientBean", QualifiedClientBean.class);
assertThat(clientBean.testBean).isSameAs(context.getBean("publicInstance"));
assertThat(clientBean.dependencyBean).isSameAs(context.getBean("dependencyBean"));
assertThat(clientBean.applicationContext).isSameAs(context);
}
Aggregations