Search in sources :

Example 1 with ThreadLocalTargetSource

use of org.springframework.aop.target.ThreadLocalTargetSource in project spring-framework by spring-projects.

the class QuickTargetSourceCreator method createBeanFactoryBasedTargetSource.

@Override
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)

Example 2 with ThreadLocalTargetSource

use of org.springframework.aop.target.ThreadLocalTargetSource 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");
    assertFalse(AopUtils.isAopProxy(test));
    assertEquals("Rod", test.getName());
    // Check that references survived pooling
    assertEquals("Kerry", test.getSpouse().getName());
    // Now test the pooled one
    test = (ITestBean) bf.getBean(":test");
    assertTrue(AopUtils.isAopProxy(test));
    Advised advised = (Advised) test;
    assertTrue(advised.getTargetSource() instanceof CommonsPool2TargetSource);
    assertEquals("Rod", test.getName());
    // Check that references survived pooling
    assertEquals("Kerry", test.getSpouse().getName());
    // Now test the ThreadLocal one
    test = (ITestBean) bf.getBean("%test");
    assertTrue(AopUtils.isAopProxy(test));
    advised = (Advised) test;
    assertTrue(advised.getTargetSource() instanceof ThreadLocalTargetSource);
    assertEquals("Rod", test.getName());
    // Check that references survived pooling
    assertEquals("Kerry", test.getSpouse().getName());
    // Now test the Prototype TargetSource
    test = (ITestBean) bf.getBean("!test");
    assertTrue(AopUtils.isAopProxy(test));
    advised = (Advised) test;
    assertTrue(advised.getTargetSource() instanceof PrototypeTargetSource);
    assertEquals("Rod", test.getName());
    // Check that references survived pooling
    assertEquals("Kerry", test.getSpouse().getName());
    ITestBean test2 = (ITestBean) bf.getBean("!test");
    assertFalse("Prototypes cannot be the same object", test == test2);
    assertEquals("Rod", test2.getName());
    assertEquals("Kerry", test2.getSpouse().getName());
    bf.close();
}
Also used : ITestBean(org.springframework.tests.sample.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.Test)

Aggregations

CommonsPool2TargetSource (org.springframework.aop.target.CommonsPool2TargetSource)2 PrototypeTargetSource (org.springframework.aop.target.PrototypeTargetSource)2 ThreadLocalTargetSource (org.springframework.aop.target.ThreadLocalTargetSource)2 Test (org.junit.Test)1 Advised (org.springframework.aop.framework.Advised)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 ITestBean (org.springframework.tests.sample.beans.ITestBean)1