use of cn.taketoday.beans.factory.config.CustomScopeConfigurer in project today-framework by TAKETODAY.
the class ScopeTests method testCustomScopeConfigurer.
@Test
void testCustomScopeConfigurer() {
CustomScopeConfigurer configurer = new CustomScopeConfigurer();
SimpleThreadScope thread = new SimpleThreadScope();
configurer.addScope("thread", thread);
try (StandardApplicationContext context = new StandardApplicationContext()) {
AbstractBeanDefinition def = BeanDefinitionBuilder.rootBeanDefinition(ScopeBean.class).getBeanDefinition();
def.setBeanName("scopeBean");
context.registerBeanDefinition(def);
def.setScope("thread");
try {
context.getBean("scopeBean");
} catch (Exception e) {
assertTrue(true);
}
context.addBeanFactoryPostProcessor(configurer);
context.refresh();
Object bean = context.getBean("scopeBean");
Object bean2 = context.getBean("scopeBean");
Assertions.assertEquals(bean, bean2);
new Thread(() -> {
Object bean21 = context.getBean("scopeBean");
System.err.println(bean21);
assertNotEquals(bean21, bean);
}).start();
context.getBeanFactory().destroyScopedBean("scopeBean");
System.err.println(bean);
assertNotEquals(bean, context.getBean("scopeBean"));
}
}
Aggregations