Search in sources :

Example 1 with PrototypeTargetSource

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;
    }
}
Also used : CommonsPool2TargetSource(org.springframework.aop.target.CommonsPool2TargetSource) PrototypeTargetSource(org.springframework.aop.target.PrototypeTargetSource) ThreadLocalTargetSource(org.springframework.aop.target.ThreadLocalTargetSource) Nullable(org.springframework.lang.Nullable)

Example 2 with PrototypeTargetSource

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();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Advised(org.springframework.aop.framework.Advised) CommonsPool2TargetSource(org.springframework.aop.target.CommonsPool2TargetSource) PrototypeTargetSource(org.springframework.aop.target.PrototypeTargetSource) ThreadLocalTargetSource(org.springframework.aop.target.ThreadLocalTargetSource) Test(org.junit.jupiter.api.Test)

Example 3 with PrototypeTargetSource

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;
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Advised(org.springframework.aop.framework.Advised) BeanFactory(org.springframework.beans.factory.BeanFactory) PrototypeTargetSource(org.springframework.aop.target.PrototypeTargetSource) Test(org.junit.jupiter.api.Test)

Aggregations

PrototypeTargetSource (org.springframework.aop.target.PrototypeTargetSource)3 Test (org.junit.jupiter.api.Test)2 Advised (org.springframework.aop.framework.Advised)2 CommonsPool2TargetSource (org.springframework.aop.target.CommonsPool2TargetSource)2 ThreadLocalTargetSource (org.springframework.aop.target.ThreadLocalTargetSource)2 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 BeanFactory (org.springframework.beans.factory.BeanFactory)1 Nullable (org.springframework.lang.Nullable)1