Search in sources :

Example 1 with CustomScopeConfigurer

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"));
    }
}
Also used : SimpleThreadScope(cn.taketoday.beans.factory.config.SimpleThreadScope) StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) AbstractBeanDefinition(cn.taketoday.beans.factory.support.AbstractBeanDefinition) CustomScopeConfigurer(cn.taketoday.beans.factory.config.CustomScopeConfigurer) Test(org.junit.jupiter.api.Test)

Aggregations

CustomScopeConfigurer (cn.taketoday.beans.factory.config.CustomScopeConfigurer)1 SimpleThreadScope (cn.taketoday.beans.factory.config.SimpleThreadScope)1 AbstractBeanDefinition (cn.taketoday.beans.factory.support.AbstractBeanDefinition)1 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)1 Test (org.junit.jupiter.api.Test)1