Search in sources :

Example 26 with Advised

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

the class MockitoAopProxyTargetInterceptor method applyTo.

@Autowired
public static void applyTo(Object source) {
    Assert.state(AopUtils.isAopProxy(source), "Source must be an AOP proxy");
    try {
        Advised advised = (Advised) source;
        for (Advisor advisor : advised.getAdvisors()) {
            if (advisor instanceof MockitoAopProxyTargetInterceptor) {
                return;
            }
        }
        Object target = AopTestUtils.getUltimateTargetObject(source);
        Advice advice = new MockitoAopProxyTargetInterceptor(source, target);
        advised.addAdvice(0, advice);
    } catch (Exception ex) {
        throw new IllegalStateException("Unable to apply Mockito AOP support", ex);
    }
}
Also used : Advised(org.springframework.aop.framework.Advised) Advisor(org.springframework.aop.Advisor) Advice(org.aopalliance.aop.Advice) Autowired(org.springframework.beans.factory.annotation.Autowired)

Example 27 with Advised

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

the class PerThisAspect method testPerTypeWithinAspect.

@Test
public void testPerTypeWithinAspect() throws SecurityException, NoSuchMethodException {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    PerTypeWithinAspectInstanceFactory aif = new PerTypeWithinAspectInstanceFactory();
    TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(aif), TestBean.class);
    assertEquals("No method calls", 0, aif.getInstantiationCount());
    assertEquals("Around advice must now apply", 0, 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();
    assertTrue(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 still apply", 1, itb.getAge());
    assertEquals("Around advice must still apply", 2, itb.getAge());
    TestBean itb2 = (TestBean) createProxy(target, getFixture().getAdvisors(aif), TestBean.class);
    assertEquals(1, aif.getInstantiationCount());
    assertEquals("Around advice be independent for second instance", 0, itb2.getAge());
    assertEquals(2, aif.getInstantiationCount());
}
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 28 with Advised

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

the class TestNamespaceHandler method testChainedDecorators.

@Test
public void testChainedDecorators() throws Exception {
    ITestBean bean = (ITestBean) this.beanFactory.getBean("chainedTestBean");
    assertTestBean(bean);
    assertTrue(AopUtils.isAopProxy(bean));
    Advisor[] advisors = ((Advised) bean).getAdvisors();
    assertEquals("Incorrect number of advisors", 2, advisors.length);
    assertEquals("Incorrect advice class", DebugInterceptor.class, advisors[0].getAdvice().getClass());
    assertEquals("Incorrect advice class", NopInterceptor.class, advisors[1].getAdvice().getClass());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) Advised(org.springframework.aop.framework.Advised) Advisor(org.springframework.aop.Advisor) Test(org.junit.Test)

Example 29 with Advised

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

the class AnnotationDrivenEventListenerTests method eventListenerWorksWithSimpleInterfaceProxy.

@Test
public void eventListenerWorksWithSimpleInterfaceProxy() throws Exception {
    load(ScopedProxyTestBean.class);
    SimpleService proxy = this.context.getBean(SimpleService.class);
    assertTrue("bean should be a proxy", proxy instanceof Advised);
    this.eventCollector.assertNoEventReceived(proxy.getId());
    this.context.publishEvent(new ContextRefreshedEvent(this.context));
    this.eventCollector.assertNoEventReceived(proxy.getId());
    TestEvent event = new TestEvent();
    this.context.publishEvent(event);
    this.eventCollector.assertEvent(proxy.getId(), event);
    this.eventCollector.assertTotalEventsCount(1);
}
Also used : Advised(org.springframework.aop.framework.Advised) TestEvent(org.springframework.context.event.test.TestEvent) AnotherTestEvent(org.springframework.context.event.test.AnotherTestEvent) Test(org.junit.Test)

Example 30 with Advised

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

the class AnnotationDrivenEventListenerTests method eventListenerWorksWithAnnotatedInterfaceProxy.

@Test
public void eventListenerWorksWithAnnotatedInterfaceProxy() throws Exception {
    load(AnnotatedProxyTestBean.class);
    AnnotatedSimpleService proxy = this.context.getBean(AnnotatedSimpleService.class);
    assertTrue("bean should be a proxy", proxy instanceof Advised);
    this.eventCollector.assertNoEventReceived(proxy.getId());
    this.context.publishEvent(new ContextRefreshedEvent(this.context));
    this.eventCollector.assertNoEventReceived(proxy.getId());
    TestEvent event = new TestEvent();
    this.context.publishEvent(event);
    this.eventCollector.assertEvent(proxy.getId(), event);
    this.eventCollector.assertTotalEventsCount(1);
}
Also used : Advised(org.springframework.aop.framework.Advised) TestEvent(org.springframework.context.event.test.TestEvent) AnotherTestEvent(org.springframework.context.event.test.AnotherTestEvent) Test(org.junit.Test)

Aggregations

Advised (org.springframework.aop.framework.Advised)35 Test (org.junit.Test)22 ITestBean (org.springframework.tests.sample.beans.ITestBean)15 Advisor (org.springframework.aop.Advisor)10 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 PrototypeTargetSource (org.springframework.aop.target.PrototypeTargetSource)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 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)2 SerializablePerson (org.springframework.tests.sample.beans.SerializablePerson)2