Search in sources :

Example 6 with SimpleMapScope

use of org.springframework.tests.context.SimpleMapScope 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);
    assertFalse(fmc.getClass().getName().contains(ClassUtils.CGLIB_CLASS_SEPARATOR));
    //2
    TestBean tb = (TestBean) context.getBean("publicInstance");
    assertEquals("publicInstance", tb.getName());
    //2
    TestBean tb2 = (TestBean) context.getBean("publicInstance");
    assertEquals("publicInstance", tb2.getName());
    assertSame(tb2, tb);
    //3
    tb = (TestBean) context.getBean("protectedInstance");
    assertEquals("protectedInstance", tb.getName());
    assertSame(tb, context.getBean("protectedInstance"));
    assertEquals("0", tb.getCountry());
    //3
    tb2 = context.getBean("protectedInstance", TestBean.class);
    assertEquals("protectedInstance", tb2.getName());
    assertSame(tb2, tb);
    //4
    tb = context.getBean("privateInstance", TestBean.class);
    assertEquals("privateInstance", tb.getName());
    assertEquals(1, tb.getAge());
    //4
    tb2 = context.getBean("privateInstance", TestBean.class);
    assertEquals(2, tb2.getAge());
    assertNotSame(tb2, tb);
    //5
    Object bean = context.getBean("requestScopedInstance");
    assertTrue(AopUtils.isCglibProxy(bean));
    assertTrue(bean instanceof ScopedObject);
    QualifiedClientBean clientBean = context.getBean("clientBean", QualifiedClientBean.class);
    assertSame(context.getBean("publicInstance"), clientBean.testBean);
    assertSame(context.getBean("dependencyBean"), clientBean.dependencyBean);
    assertSame(context, clientBean.applicationContext);
}
Also used : ScopedObject(org.springframework.aop.scope.ScopedObject) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) SimpleMapScope(org.springframework.tests.context.SimpleMapScope) TestBean(org.springframework.tests.sample.beans.TestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ScopedObject(org.springframework.aop.scope.ScopedObject) FactoryMethodComponent(org.springframework.context.annotation4.FactoryMethodComponent) Test(org.junit.Test)

Example 7 with SimpleMapScope

use of org.springframework.tests.context.SimpleMapScope in project spring-framework by spring-projects.

the class ComponentScanWithBasePackagesAndValueAlias method withScopedProxyThroughRegex.

@Test
public void withScopedProxyThroughRegex() throws IOException, ClassNotFoundException {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ComponentScanWithScopedProxyThroughRegex.class);
    ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
    ctx.refresh();
    // should cast to the interface
    FooService bean = (FooService) ctx.getBean("scopedProxyTestBean");
    // should be dynamic proxy
    assertThat(AopUtils.isJdkDynamicProxy(bean), is(true));
}
Also used : FooService(example.scannable.FooService) SimpleMapScope(org.springframework.tests.context.SimpleMapScope) Test(org.junit.Test)

Example 8 with SimpleMapScope

use of org.springframework.tests.context.SimpleMapScope in project spring-framework by spring-projects.

the class ComponentScanWithBasePackagesAndValueAlias method withScopedProxyThroughAspectJPattern.

@Test
public void withScopedProxyThroughAspectJPattern() throws IOException, ClassNotFoundException {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ComponentScanWithScopedProxyThroughAspectJPattern.class);
    ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope());
    ctx.refresh();
    // should cast to the interface
    FooService bean = (FooService) ctx.getBean("scopedProxyTestBean");
    // should be dynamic proxy
    assertThat(AopUtils.isJdkDynamicProxy(bean), is(true));
}
Also used : FooService(example.scannable.FooService) SimpleMapScope(org.springframework.tests.context.SimpleMapScope) Test(org.junit.Test)

Example 9 with SimpleMapScope

use of org.springframework.tests.context.SimpleMapScope in project spring-framework by spring-projects.

the class ScopedProxyTests method testJdkScopedProxy.

@Test
public void testJdkScopedProxy() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(TESTBEAN_CONTEXT);
    bf.setSerializationId("X");
    SimpleMapScope scope = new SimpleMapScope();
    bf.registerScope("request", scope);
    ITestBean bean = (ITestBean) bf.getBean("testBean");
    assertNotNull(bean);
    assertTrue(AopUtils.isJdkDynamicProxy(bean));
    assertTrue(bean instanceof ScopedObject);
    ScopedObject scoped = (ScopedObject) bean;
    assertEquals(TestBean.class, scoped.getTargetObject().getClass());
    bean.setAge(101);
    assertTrue(scope.getMap().containsKey("testBeanTarget"));
    assertEquals(TestBean.class, scope.getMap().get("testBeanTarget").getClass());
    ITestBean deserialized = (ITestBean) SerializationTestUtils.serializeAndDeserialize(bean);
    assertNotNull(deserialized);
    assertTrue(AopUtils.isJdkDynamicProxy(deserialized));
    assertEquals(101, bean.getAge());
    assertTrue(deserialized instanceof ScopedObject);
    ScopedObject scopedDeserialized = (ScopedObject) deserialized;
    assertEquals(TestBean.class, scopedDeserialized.getTargetObject().getClass());
    bf.setSerializationId(null);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SimpleMapScope(org.springframework.tests.context.SimpleMapScope) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Example 10 with SimpleMapScope

use of org.springframework.tests.context.SimpleMapScope in project spring-framework by spring-projects.

the class ComponentScanParserScopedProxyTests method testDefaultScopedProxy.

@Test
public void testDefaultScopedProxy() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("org/springframework/context/annotation/scopedProxyDefaultTests.xml");
    context.getBeanFactory().registerScope("myScope", new SimpleMapScope());
    ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean");
    // should not be a proxy
    assertFalse(AopUtils.isAopProxy(bean));
    context.close();
}
Also used : SimpleMapScope(org.springframework.tests.context.SimpleMapScope) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ScopedProxyTestBean(example.scannable.ScopedProxyTestBean) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 SimpleMapScope (org.springframework.tests.context.SimpleMapScope)12 FooService (example.scannable.FooService)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 ScopedProxyTestBean (example.scannable.ScopedProxyTestBean)3 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)3 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)3 ITestBean (org.springframework.tests.sample.beans.ITestBean)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 ArrayList (java.util.ArrayList)1 ScopedObject (org.springframework.aop.scope.ScopedObject)1 FactoryMethodComponent (org.springframework.context.annotation4.FactoryMethodComponent)1