use of org.springframework.aop.target.PrototypeTargetSource in project spring-framework by spring-projects.
the class QuickTargetSourceCreator method createBeanFactoryBasedTargetSource.
@Override
@Nullable
protected final AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource(Class<?> beanClass, String beanName) {
if (beanName.startsWith(PREFIX_COMMONS_POOL)) {
CommonsPool2TargetSource cpts = new CommonsPool2TargetSource();
cpts.setMaxSize(25);
return cpts;
} else if (beanName.startsWith(PREFIX_THREAD_LOCAL)) {
return new ThreadLocalTargetSource();
} else if (beanName.startsWith(PREFIX_PROTOTYPE)) {
return new PrototypeTargetSource();
} else {
// No match. Don't create a custom target source.
return null;
}
}
use of org.springframework.aop.target.PrototypeTargetSource in project spring-framework by spring-projects.
the class SelectivePrototypeTargetSourceCreator method testQuickTargetSourceCreator.
@Test
public void testQuickTargetSourceCreator() throws Exception {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(QUICK_TARGETSOURCE_CONTEXT, CLASS);
ITestBean test = (ITestBean) bf.getBean("test");
assertThat(AopUtils.isAopProxy(test)).isFalse();
assertThat(test.getName()).isEqualTo("Rod");
// Check that references survived pooling
assertThat(test.getSpouse().getName()).isEqualTo("Kerry");
// Now test the pooled one
test = (ITestBean) bf.getBean(":test");
assertThat(AopUtils.isAopProxy(test)).isTrue();
Advised advised = (Advised) test;
boolean condition2 = advised.getTargetSource() instanceof CommonsPool2TargetSource;
assertThat(condition2).isTrue();
assertThat(test.getName()).isEqualTo("Rod");
// Check that references survived pooling
assertThat(test.getSpouse().getName()).isEqualTo("Kerry");
// Now test the ThreadLocal one
test = (ITestBean) bf.getBean("%test");
assertThat(AopUtils.isAopProxy(test)).isTrue();
advised = (Advised) test;
boolean condition1 = advised.getTargetSource() instanceof ThreadLocalTargetSource;
assertThat(condition1).isTrue();
assertThat(test.getName()).isEqualTo("Rod");
// Check that references survived pooling
assertThat(test.getSpouse().getName()).isEqualTo("Kerry");
// Now test the Prototype TargetSource
test = (ITestBean) bf.getBean("!test");
assertThat(AopUtils.isAopProxy(test)).isTrue();
advised = (Advised) test;
boolean condition = advised.getTargetSource() instanceof PrototypeTargetSource;
assertThat(condition).isTrue();
assertThat(test.getName()).isEqualTo("Rod");
// Check that references survived pooling
assertThat(test.getSpouse().getName()).isEqualTo("Kerry");
ITestBean test2 = (ITestBean) bf.getBean("!test");
assertThat(test == test2).as("Prototypes cannot be the same object").isFalse();
assertThat(test2.getName()).isEqualTo("Rod");
assertThat(test2.getSpouse().getName()).isEqualTo("Kerry");
bf.close();
}
use of org.springframework.aop.target.PrototypeTargetSource in project spring-framework by spring-projects.
the class SelectivePrototypeTargetSourceCreator method testCustomPrototypeTargetSource.
@Test
public void testCustomPrototypeTargetSource() throws Exception {
CountingTestBean.count = 0;
BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
ITestBean test = (ITestBean) bf.getBean("prototypeTest");
assertThat(AopUtils.isAopProxy(test)).isTrue();
Advised advised = (Advised) test;
boolean condition = advised.getTargetSource() instanceof PrototypeTargetSource;
assertThat(condition).isTrue();
assertThat(test.getName()).isEqualTo("Rod");
// Check that references survived prototype creation
assertThat(test.getSpouse().getName()).isEqualTo("Kerry");
assertThat(CountingTestBean.count).as("Only 2 CountingTestBeans instantiated").isEqualTo(2);
CountingTestBean.count = 0;
}
Aggregations