Search in sources :

Example 1 with HotSwappableTargetSource

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

the class AbstractAopProxyTests method testExistingProxyChangesTarget.

@Test
public void testExistingProxyChangesTarget() throws Throwable {
    TestBean tb1 = new TestBean();
    tb1.setAge(33);
    TestBean tb2 = new TestBean();
    tb2.setAge(26);
    tb2.setName("Juergen");
    TestBean tb3 = new TestBean();
    tb3.setAge(37);
    ProxyFactory pc = new ProxyFactory(tb1);
    NopInterceptor nop = new NopInterceptor();
    pc.addAdvice(nop);
    ITestBean proxy = (ITestBean) createProxy(pc);
    assertEquals(nop.getCount(), 0);
    assertEquals(tb1.getAge(), proxy.getAge());
    assertEquals(nop.getCount(), 1);
    // Change to a new static target
    pc.setTarget(tb2);
    assertEquals(tb2.getAge(), proxy.getAge());
    assertEquals(nop.getCount(), 2);
    // Change to a new dynamic target
    HotSwappableTargetSource hts = new HotSwappableTargetSource(tb3);
    pc.setTargetSource(hts);
    assertEquals(tb3.getAge(), proxy.getAge());
    assertEquals(nop.getCount(), 3);
    hts.swap(tb1);
    assertEquals(tb1.getAge(), proxy.getAge());
    tb1.setName("Colin");
    assertEquals(tb1.getName(), proxy.getName());
    assertEquals(nop.getCount(), 5);
    // Change back, relying on casting to Advised
    Advised advised = (Advised) proxy;
    assertSame(hts, advised.getTargetSource());
    SingletonTargetSource sts = new SingletonTargetSource(tb2);
    advised.setTargetSource(sts);
    assertEquals(tb2.getName(), proxy.getName());
    assertSame(sts, advised.getTargetSource());
    assertEquals(tb2.getAge(), proxy.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) HotSwappableTargetSource(org.springframework.aop.target.HotSwappableTargetSource) Test(org.junit.Test)

Example 2 with HotSwappableTargetSource

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

the class BeanFactoryTransactionTests method testDynamicTargetSource.

/**
	 * Test that we can set the target to a dynamic TargetSource.
	 */
@Test
public void testDynamicTargetSource() throws NoSuchMethodException {
    // Install facade
    CallCountingTransactionManager txMan = new CallCountingTransactionManager();
    PlatformTransactionManagerFacade.delegate = txMan;
    TestBean tb = (TestBean) factory.getBean("hotSwapped");
    assertEquals(666, tb.getAge());
    int newAge = 557;
    tb.setAge(newAge);
    assertEquals(newAge, tb.getAge());
    TestBean target2 = new TestBean();
    target2.setAge(65);
    HotSwappableTargetSource ts = (HotSwappableTargetSource) factory.getBean("swapper");
    ts.swap(target2);
    assertEquals(target2.getAge(), tb.getAge());
    tb.setAge(newAge);
    assertEquals(newAge, target2.getAge());
    assertEquals(0, txMan.inflight);
    assertEquals(2, txMan.commits);
    assertEquals(0, txMan.rollbacks);
}
Also used : DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) HotSwappableTargetSource(org.springframework.aop.target.HotSwappableTargetSource) CallCountingTransactionManager(org.springframework.tests.transaction.CallCountingTransactionManager) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 HotSwappableTargetSource (org.springframework.aop.target.HotSwappableTargetSource)2 ITestBean (org.springframework.tests.sample.beans.ITestBean)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 SingletonTargetSource (org.springframework.aop.target.SingletonTargetSource)1 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)1 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)1 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)1 CallCountingTransactionManager (org.springframework.tests.transaction.CallCountingTransactionManager)1