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