Search in sources :

Example 11 with Advised

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

the class PerThisAspect method testPerTargetAspect.

@Test
public void testPerTargetAspect() throws SecurityException, NoSuchMethodException {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean")), TestBean.class);
    assertEquals("Around advice must NOT apply", realAge, itb.getAge());
    Advised advised = (Advised) itb;
    SyntheticInstantiationAdvisor sia = (SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
    assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
    InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[3];
    LazySingletonAspectInstanceFactoryDecorator maaif = (LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
    assertFalse(maaif.isMaterialized());
    // Check that the perclause pointcut is valid
    assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
    assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());
    // Hit the method in the per clause to instantiate the aspect
    itb.getSpouse();
    assertTrue(maaif.isMaterialized());
    assertEquals("Around advice must apply", 0, itb.getAge());
    assertEquals("Around advice must apply", 1, itb.getAge());
}
Also used : PerTargetAspect(test.aop.PerTargetAspect) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Advised(org.springframework.aop.framework.Advised) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) SyntheticInstantiationAdvisor(org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) Test(org.junit.Test)

Example 12 with Advised

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

the class PerThisAspect method testPerThisAspect.

@Test
public void testPerThisAspect() throws SecurityException, NoSuchMethodException {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerThisAspect(), "someBean")), TestBean.class);
    assertEquals("Around advice must NOT apply", realAge, itb.getAge());
    Advised advised = (Advised) itb;
    // Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
    assertEquals(4, advised.getAdvisors().length);
    SyntheticInstantiationAdvisor sia = (SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
    assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
    InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
    LazySingletonAspectInstanceFactoryDecorator maaif = (LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
    assertFalse(maaif.isMaterialized());
    // Check that the perclause pointcut is valid
    assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
    assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());
    // Hit the method in the per clause to instantiate the aspect
    itb.getSpouse();
    assertTrue(maaif.isMaterialized());
    assertTrue(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null));
    assertEquals("Around advice must apply", 0, itb.getAge());
    assertEquals("Around advice must apply", 1, itb.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Advised(org.springframework.aop.framework.Advised) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) SyntheticInstantiationAdvisor(org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) Test(org.junit.Test)

Example 13 with Advised

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

the class CounterAspect method testProgrammaticProxyCreation.

@Test
public void testProgrammaticProxyCreation() {
    ITestBean testBean = new TestBean();
    AspectJProxyFactory factory = new AspectJProxyFactory();
    factory.setTarget(testBean);
    CounterAspect myCounterAspect = new CounterAspect();
    factory.addAspect(myCounterAspect);
    ITestBean proxyTestBean = factory.getProxy();
    assertTrue("Expected a proxy", proxyTestBean instanceof Advised);
    proxyTestBean.setAge(20);
    assertEquals("Programmatically created proxy shouldn't match bean()", 0, myCounterAspect.count);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) AspectJProxyFactory(org.springframework.aop.aspectj.annotation.AspectJProxyFactory) Advised(org.springframework.aop.framework.Advised) Test(org.junit.Test)

Example 14 with Advised

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

the class TraceBeforeAdvice method testAfterReturningAdviceWithoutJoinPoint.

private long testAfterReturningAdviceWithoutJoinPoint(String file, int howmany, String technology) {
    ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file, CLASS);
    StopWatch sw = new StopWatch();
    sw.start(howmany + " repeated after returning advice invocations with " + technology);
    ITestBean adrian = (ITestBean) bf.getBean("adrian");
    assertTrue(AopUtils.isAopProxy(adrian));
    Advised a = (Advised) adrian;
    assertTrue(a.getAdvisors().length >= 3);
    // Hits joinpoint
    adrian.setAge(25);
    for (int i = 0; i < howmany; i++) {
        adrian.setAge(i);
    }
    sw.stop();
    System.out.println(sw.prettyPrint());
    return sw.getLastTaskTimeMillis();
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Advised(org.springframework.aop.framework.Advised) StopWatch(org.springframework.util.StopWatch)

Example 15 with Advised

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

the class TraceBeforeAdvice method testMix.

private long testMix(String file, int howmany, String technology) {
    ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file, CLASS);
    StopWatch sw = new StopWatch();
    sw.start(howmany + " repeated mixed invocations with " + technology);
    ITestBean adrian = (ITestBean) bf.getBean("adrian");
    assertTrue(AopUtils.isAopProxy(adrian));
    Advised a = (Advised) adrian;
    assertTrue(a.getAdvisors().length >= 3);
    for (int i = 0; i < howmany; i++) {
        // Hit all 3 joinpoints
        adrian.getAge();
        adrian.getName();
        adrian.setAge(i);
        // Invoke three non-advised methods
        adrian.getDoctor();
        adrian.getLawyer();
        adrian.getSpouse();
    }
    sw.stop();
    System.out.println(sw.prettyPrint());
    return sw.getLastTaskTimeMillis();
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Advised(org.springframework.aop.framework.Advised) StopWatch(org.springframework.util.StopWatch)

Aggregations

Advised (org.springframework.aop.framework.Advised)38 Test (org.junit.Test)22 ITestBean (org.springframework.tests.sample.beans.ITestBean)15 Advisor (org.springframework.aop.Advisor)11 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)10 TestBean (org.springframework.tests.sample.beans.TestBean)4 JoinPoint (org.aspectj.lang.JoinPoint)3 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)3 SyntheticInstantiationAdvisor (org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor)3 ProxyFactory (org.springframework.aop.framework.ProxyFactory)3 ApplicationContext (org.springframework.context.ApplicationContext)3 Person (org.springframework.tests.sample.beans.Person)3 StopWatch (org.springframework.util.StopWatch)3 Advice (org.aopalliance.aop.Advice)2 PrototypeTargetSource (org.springframework.aop.target.PrototypeTargetSource)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 AnotherTestEvent (org.springframework.context.event.test.AnotherTestEvent)2 TestEvent (org.springframework.context.event.test.TestEvent)2 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)2