Search in sources :

Example 21 with Advisor

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

the class AbstractAutoProxyCreator method resolveInterceptorNames.

/**
 * Resolves the specified interceptor names to Advisor objects.
 * @see #setInterceptorNames
 */
private Advisor[] resolveInterceptorNames() {
    BeanFactory bf = this.beanFactory;
    ConfigurableBeanFactory cbf = (bf instanceof ConfigurableBeanFactory ? (ConfigurableBeanFactory) bf : null);
    List<Advisor> advisors = new ArrayList<>();
    for (String beanName : this.interceptorNames) {
        if (cbf == null || !cbf.isCurrentlyInCreation(beanName)) {
            Assert.state(bf != null, "BeanFactory required for resolving interceptor names");
            Object next = bf.getBean(beanName);
            advisors.add(this.advisorAdapterRegistry.wrap(next));
        }
    }
    return advisors.toArray(new Advisor[0]);
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) ArrayList(java.util.ArrayList) Advisor(org.springframework.aop.Advisor)

Example 22 with Advisor

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

the class AspectJPrecedenceComparatorTests method testLowerAdvisorPrecedenceAfterAdvice.

@Test
public void testLowerAdvisorPrecedenceAfterAdvice() {
    Advisor advisor1 = createAspectJAfterAdvice(LOW_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect");
    Advisor advisor2 = createAspectJAroundAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someOtherAspect");
    assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor1 sorted after advisor2").isEqualTo(1);
    advisor1 = createSpringAOPAfterAdvice(LOW_PRECEDENCE_ADVISOR_ORDER);
    advisor2 = createAspectJAfterThrowingAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect");
    assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor1 sorted after advisor2").isEqualTo(1);
}
Also used : DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) AspectJPointcutAdvisor(org.springframework.aop.aspectj.AspectJPointcutAdvisor) Advisor(org.springframework.aop.Advisor) Test(org.junit.jupiter.api.Test)

Example 23 with Advisor

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

the class AspectJPrecedenceComparatorTests method testLowerAdvisorPrecedenceNoAfterAdvice.

@Test
public void testLowerAdvisorPrecedenceNoAfterAdvice() {
    Advisor advisor1 = createAspectJBeforeAdvice(LOW_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect");
    Advisor advisor2 = createAspectJBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect");
    assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor1 sorted after advisor2").isEqualTo(1);
    advisor1 = createAspectJBeforeAdvice(LOW_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect");
    advisor2 = createAspectJAroundAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect");
    assertThat(this.comparator.compare(advisor1, advisor2)).as("advisor1 sorted after advisor2").isEqualTo(1);
}
Also used : DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) AspectJPointcutAdvisor(org.springframework.aop.aspectj.AspectJPointcutAdvisor) Advisor(org.springframework.aop.Advisor) Test(org.junit.jupiter.api.Test)

Example 24 with Advisor

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

the class AspectJPrecedenceComparatorTests method createSpringAOPAfterAdvice.

private Advisor createSpringAOPAfterAdvice(int order) {
    AfterReturningAdvice advice = (returnValue, method, args, target) -> {
    };
    DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(this.anyOldPointcut, advice);
    advisor.setOrder(order);
    return advisor;
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Test(org.junit.jupiter.api.Test) AspectJAfterAdvice(org.springframework.aop.aspectj.AspectJAfterAdvice) AspectJAfterReturningAdvice(org.springframework.aop.aspectj.AspectJAfterReturningAdvice) AspectJAroundAdvice(org.springframework.aop.aspectj.AspectJAroundAdvice) AspectJAfterThrowingAdvice(org.springframework.aop.aspectj.AspectJAfterThrowingAdvice) AspectJMethodBeforeAdvice(org.springframework.aop.aspectj.AspectJMethodBeforeAdvice) AspectJPointcutAdvisor(org.springframework.aop.aspectj.AspectJPointcutAdvisor) BeforeAdvice(org.springframework.aop.BeforeAdvice) AbstractAspectJAdvice(org.springframework.aop.aspectj.AbstractAspectJAdvice) Method(java.lang.reflect.Method) AspectJExpressionPointcut(org.springframework.aop.aspectj.AspectJExpressionPointcut) Advisor(org.springframework.aop.Advisor) AfterReturningAdvice(org.springframework.aop.AfterReturningAdvice) AspectJAfterReturningAdvice(org.springframework.aop.aspectj.AspectJAfterReturningAdvice) AfterReturningAdvice(org.springframework.aop.AfterReturningAdvice) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor)

Example 25 with Advisor

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

the class ProxyFactoryTests method testIndexOfMethods.

@Test
public void testIndexOfMethods() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
    Advised advised = (Advised) pf.getProxy();
    // Can use advised and ProxyFactory interchangeably
    advised.addAdvice(nop);
    pf.addAdvisor(advisor);
    assertThat(pf.indexOf(new NopInterceptor())).isEqualTo(-1);
    assertThat(pf.indexOf(nop)).isEqualTo(0);
    assertThat(pf.indexOf(advisor)).isEqualTo(1);
    assertThat(advised.indexOf(new DefaultPointcutAdvisor(null))).isEqualTo(-1);
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Aggregations

Advisor (org.springframework.aop.Advisor)70 Test (org.junit.jupiter.api.Test)33 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)25 Advised (org.springframework.aop.framework.Advised)21 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)19 Test (org.junit.Test)16 TestBean (org.springframework.beans.testfixture.beans.TestBean)16 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)14 AspectJPointcutAdvisor (org.springframework.aop.aspectj.AspectJPointcutAdvisor)11 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)11 ArrayList (java.util.ArrayList)10 JoinPoint (org.aspectj.lang.JoinPoint)8 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)8 Method (java.lang.reflect.Method)7 SyntheticInstantiationAdvisor (org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor)7 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)7 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)7 LockMixinAdvisor (test.mixin.LockMixinAdvisor)7 Advice (org.aopalliance.aop.Advice)6 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)6