Search in sources :

Example 46 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 47 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 48 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 49 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)

Example 50 with Advised

use of org.springframework.aop.framework.Advised in project cxf by apache.

the class SpringAopClassHelper method getRealClassInternal.

protected Class<?> getRealClassInternal(Object o) {
    if (AopUtils.isAopProxy(o) && (o instanceof Advised)) {
        Advised advised = (Advised) o;
        try {
            TargetSource targetSource = advised.getTargetSource();
            Object target = null;
            try {
                target = targetSource.getTarget();
            } catch (BeanCreationException ex) {
                // be active on the current thread yet
                return getRealClassFromClassInternal(targetSource.getTargetClass());
            }
            if (target == null) {
                Class<?> targetClass = AopUtils.getTargetClass(o);
                if (targetClass != null) {
                    return getRealClassFromClassInternal(targetClass);
                }
            } else {
                return getRealClassInternal(target);
            }
        } catch (Exception ex) {
        // ignore
        }
    } else if (ClassUtils.isCglibProxyClass(o.getClass())) {
        return getRealClassFromClassInternal(AopUtils.getTargetClass(o));
    }
    return o.getClass();
}
Also used : TargetSource(org.springframework.aop.TargetSource) BeanCreationException(org.springframework.beans.factory.BeanCreationException) Advised(org.springframework.aop.framework.Advised) BeanCreationException(org.springframework.beans.factory.BeanCreationException)

Aggregations

Advised (org.springframework.aop.framework.Advised)78 Advisor (org.springframework.aop.Advisor)21 Test (org.junit.jupiter.api.Test)20 Test (org.junit.Test)18 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)16 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)13 ProxyFactory (org.springframework.aop.framework.ProxyFactory)10 Advice (org.aopalliance.aop.Advice)8 JoinPoint (org.aspectj.lang.JoinPoint)6 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)6 TargetSource (org.springframework.aop.TargetSource)6 BeanCreationException (org.springframework.beans.factory.BeanCreationException)6 MessageChannel (org.springframework.messaging.MessageChannel)6 TestBean (org.springframework.beans.testfixture.beans.TestBean)4 ChannelAccessPolicy (org.springframework.integration.security.channel.ChannelAccessPolicy)4 ChannelSecurityInterceptor (org.springframework.integration.security.channel.ChannelSecurityInterceptor)4 ConfigAttribute (org.springframework.security.access.ConfigAttribute)4 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 List (java.util.List)3