Search in sources :

Example 1 with AfterReturningAdvice

use of org.springframework.aop.AfterReturningAdvice 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 2 with AfterReturningAdvice

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

the class AbstractAopProxyTests method testAfterReturningAdvisorIsInvoked.

@Test
public void testAfterReturningAdvisorIsInvoked() {
    class SummingAfterAdvice implements AfterReturningAdvice {

        public int sum;

        @Override
        public void afterReturning(@Nullable Object returnValue, Method m, Object[] args, @Nullable Object target) throws Throwable {
            sum += ((Integer) returnValue).intValue();
        }
    }
    SummingAfterAdvice aa = new SummingAfterAdvice();
    @SuppressWarnings("serial") Advisor matchesInt = new StaticMethodMatcherPointcutAdvisor(aa) {

        @Override
        public boolean matches(Method m, @Nullable Class<?> targetClass) {
            return m.getReturnType() == int.class;
        }
    };
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvisor(matchesInt);
    assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesInt);
    ITestBean proxied = (ITestBean) createProxy(pf);
    assertThat(aa.sum).isEqualTo(0);
    int i1 = 12;
    int i2 = 13;
    // Won't be advised
    proxied.setAge(i1);
    assertThat(proxied.getAge()).isEqualTo(i1);
    assertThat(aa.sum).isEqualTo(i1);
    proxied.setAge(i2);
    assertThat(proxied.getAge()).isEqualTo(i2);
    assertThat(aa.sum).isEqualTo((i1 + i2));
    assertThat(proxied.getAge()).isEqualTo(i2);
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) LockMixinAdvisor(test.mixin.LockMixinAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) CountingAfterReturningAdvice(org.springframework.aop.testfixture.advice.CountingAfterReturningAdvice) AfterReturningAdvice(org.springframework.aop.AfterReturningAdvice) Method(java.lang.reflect.Method) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) Nullable(org.springframework.lang.Nullable) Test(org.junit.jupiter.api.Test)

Aggregations

Method (java.lang.reflect.Method)2 Test (org.junit.jupiter.api.Test)2 Advisor (org.springframework.aop.Advisor)2 AfterReturningAdvice (org.springframework.aop.AfterReturningAdvice)2 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 BeforeAdvice (org.springframework.aop.BeforeAdvice)1 AbstractAspectJAdvice (org.springframework.aop.aspectj.AbstractAspectJAdvice)1 AspectJAfterAdvice (org.springframework.aop.aspectj.AspectJAfterAdvice)1 AspectJAfterReturningAdvice (org.springframework.aop.aspectj.AspectJAfterReturningAdvice)1 AspectJAfterThrowingAdvice (org.springframework.aop.aspectj.AspectJAfterThrowingAdvice)1 AspectJAroundAdvice (org.springframework.aop.aspectj.AspectJAroundAdvice)1 AspectJExpressionPointcut (org.springframework.aop.aspectj.AspectJExpressionPointcut)1 AspectJMethodBeforeAdvice (org.springframework.aop.aspectj.AspectJMethodBeforeAdvice)1 AspectJPointcutAdvisor (org.springframework.aop.aspectj.AspectJPointcutAdvisor)1 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)1 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)1 CountingAfterReturningAdvice (org.springframework.aop.testfixture.advice.CountingAfterReturningAdvice)1 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)1