Search in sources :

Example 1 with FactoryMethodComponent

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);
}
Also used : ScopedObject(org.springframework.aop.scope.ScopedObject) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) SimpleMapScope(org.springframework.context.testfixture.SimpleMapScope) TestBean(org.springframework.beans.testfixture.beans.TestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ScopedObject(org.springframework.aop.scope.ScopedObject) FactoryMethodComponent(org.springframework.context.annotation4.FactoryMethodComponent) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)1 ScopedObject (org.springframework.aop.scope.ScopedObject)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 TestBean (org.springframework.beans.testfixture.beans.TestBean)1 FactoryMethodComponent (org.springframework.context.annotation4.FactoryMethodComponent)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 SimpleMapScope (org.springframework.context.testfixture.SimpleMapScope)1