Search in sources :

Example 1 with SimpleThreadScope

use of cn.taketoday.beans.factory.config.SimpleThreadScope in project today-framework by TAKETODAY.

the class MapperScannerConfigurerTest method setupContext.

@BeforeEach
void setupContext() {
    applicationContext = new GenericApplicationContext();
    // add the mapper scanner as a bean definition rather than explicitly setting a
    // postProcessor on the context so initialization follows the same code path as reading from
    // an XML config file
    BeanDefinition definition = new BeanDefinition(MapperScannerConfigurer.class);
    definition.propertyValues().add("basePackage", "cn.taketoday.orm.mybatis.mapper");
    applicationContext.registerBeanDefinition("mapperScanner", definition);
    applicationContext.getBeanFactory().registerScope("thread", new SimpleThreadScope());
    setupSqlSessionFactory("sqlSessionFactory");
// assume support for autowiring fields is added by MapperScannerConfigurer via
// cn.taketoday.context.annotation.ClassPathBeanDefinitionScanner.includeAnnotationConfig
}
Also used : SimpleThreadScope(cn.taketoday.beans.factory.config.SimpleThreadScope) GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with SimpleThreadScope

use of cn.taketoday.beans.factory.config.SimpleThreadScope 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)

Example 3 with SimpleThreadScope

use of cn.taketoday.beans.factory.config.SimpleThreadScope in project today-framework by TAKETODAY.

the class ScopeTests method testSimpleThreadScope.

@Test
void testSimpleThreadScope() {
    SimpleThreadScope thread = new SimpleThreadScope();
    try (StandardApplicationContext context = new StandardApplicationContext()) {
        context.getBeanFactory().registerScope("thread", thread);
        AbstractBeanDefinition def = BeanDefinitionBuilder.rootBeanDefinition(ScopeBean.class).getBeanDefinition();
        def.setBeanName("scopeBean");
        context.registerBeanDefinition(def);
        def.setScope("thread");
        context.refresh();
        Object bean = context.getBean("scopeBean");
        Object bean2 = context.getBean("scopeBean");
        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);
        Assertions.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) Test(org.junit.jupiter.api.Test)

Example 4 with SimpleThreadScope

use of cn.taketoday.beans.factory.config.SimpleThreadScope in project today-framework by TAKETODAY.

the class MapperScanTest method setupContext.

@BeforeEach
void setupContext() {
    applicationContext = new StandardApplicationContext();
    applicationContext.getBeanFactory().registerScope("thread", new SimpleThreadScope());
    setupSqlSessionFactory();
// assume support for autowiring fields is added by MapperScannerConfigurer
// via
// cn.taketoday.context.annotation.ClassPathBeanDefinitionScanner.includeAnnotationConfig
}
Also used : SimpleThreadScope(cn.taketoday.beans.factory.config.SimpleThreadScope) StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

SimpleThreadScope (cn.taketoday.beans.factory.config.SimpleThreadScope)4 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)3 AbstractBeanDefinition (cn.taketoday.beans.factory.support.AbstractBeanDefinition)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)1 CustomScopeConfigurer (cn.taketoday.beans.factory.config.CustomScopeConfigurer)1 GenericApplicationContext (cn.taketoday.context.support.GenericApplicationContext)1