Search in sources :

Example 71 with Advised

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

the class EnableAsyncTests method customAsyncAnnotationIsPropagated.

@Test
public void customAsyncAnnotationIsPropagated() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(CustomAsyncAnnotationConfig.class, CustomAsyncBean.class);
    ctx.refresh();
    Object bean = ctx.getBean(CustomAsyncBean.class);
    assertThat(AopUtils.isAopProxy(bean)).isTrue();
    boolean isAsyncAdvised = false;
    for (Advisor advisor : ((Advised) bean).getAdvisors()) {
        if (advisor instanceof AsyncAnnotationAdvisor) {
            isAsyncAdvised = true;
            break;
        }
    }
    assertThat(isAsyncAdvised).as("bean was not async advised as expected").isTrue();
    ctx.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Advised(org.springframework.aop.framework.Advised) Advisor(org.springframework.aop.Advisor) Test(org.junit.jupiter.api.Test)

Example 72 with Advised

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

the class PerThisAspect method perTypeWithinAspect.

@Test
void perTypeWithinAspect() throws Exception {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    PerTypeWithinAspectInstanceFactory aif = new PerTypeWithinAspectInstanceFactory();
    TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(aif), TestBean.class);
    assertThat(aif.getInstantiationCount()).as("No method calls").isEqualTo(0);
    assertThat(itb.getAge()).as("Around advice must now apply").isEqualTo(0);
    Advised advised = (Advised) itb;
    // Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
    assertThat(advised.getAdvisors().length).isEqualTo(4);
    ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia = (ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
    assertThat(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)).isTrue();
    InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
    LazySingletonAspectInstanceFactoryDecorator maaif = (LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
    assertThat(maaif.isMaterialized()).isTrue();
    // Check that the perclause pointcut is valid
    assertThat(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)).isTrue();
    assertThat(imapa.getPointcut()).isNotSameAs(imapa.getDeclaredPointcut());
    // Hit the method in the per clause to instantiate the aspect
    itb.getSpouse();
    assertThat(maaif.isMaterialized()).isTrue();
    assertThat(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null)).isTrue();
    assertThat(itb.getAge()).as("Around advice must still apply").isEqualTo(1);
    assertThat(itb.getAge()).as("Around advice must still apply").isEqualTo(2);
    TestBean itb2 = (TestBean) createProxy(target, getFixture().getAdvisors(aif), TestBean.class);
    assertThat(aif.getInstantiationCount()).isEqualTo(1);
    assertThat(itb2.getAge()).as("Around advice be independent for second instance").isEqualTo(0);
    assertThat(aif.getInstantiationCount()).isEqualTo(2);
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Advised(org.springframework.aop.framework.Advised) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.jupiter.api.Test)

Example 73 with Advised

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

the class PerThisAspect method perTargetAspect.

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

Example 74 with Advised

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

the class RegexpMethodPointcutAdvisorIntegrationTests method testSerialization.

@Test
public void testSerialization() throws Throwable {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
    // This is a CGLIB proxy, so we can proxy it to the target class
    Person p = (Person) bf.getBean("serializableSettersAdvised");
    // Interceptor behind regexp advisor
    NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
    assertThat(nop.getCount()).isEqualTo(0);
    int newAge = 12;
    // Not advised
    assertThat(p.getAge()).isEqualTo(0);
    assertThat(nop.getCount()).isEqualTo(0);
    // This is proxied
    p.setAge(newAge);
    assertThat(nop.getCount()).isEqualTo(1);
    p.setAge(newAge);
    assertThat(p.getAge()).isEqualTo(newAge);
    // Only setter fired
    assertThat(nop.getCount()).isEqualTo(2);
    // Serialize and continue...
    p = SerializationTestUtils.serializeAndDeserialize(p);
    assertThat(p.getAge()).isEqualTo(newAge);
    // Remembers count, but we need to get a new reference to nop...
    nop = (SerializableNopInterceptor) ((Advised) p).getAdvisors()[0].getAdvice();
    assertThat(nop.getCount()).isEqualTo(2);
    assertThat(p.getName()).isEqualTo("serializableSettersAdvised");
    p.setAge(newAge + 1);
    assertThat(nop.getCount()).isEqualTo(3);
    assertThat(p.getAge()).isEqualTo((newAge + 1));
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) Advised(org.springframework.aop.framework.Advised) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Person(org.springframework.beans.testfixture.beans.Person) Test(org.junit.jupiter.api.Test)

Example 75 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);
    assertThat(AopUtils.isAopProxy(bean)).isTrue();
    Advisor[] advisors = ((Advised) bean).getAdvisors();
    assertThat(advisors.length).as("Incorrect number of advisors").isEqualTo(2);
    assertThat(advisors[0].getAdvice().getClass()).as("Incorrect advice class").isEqualTo(DebugInterceptor.class);
    assertThat(advisors[1].getAdvice().getClass()).as("Incorrect advice class").isEqualTo(NopInterceptor.class);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Advised(org.springframework.aop.framework.Advised) Advisor(org.springframework.aop.Advisor) Test(org.junit.jupiter.api.Test)

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