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);
}
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));
}
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));
}
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);
}
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();
}
Aggregations