简体   繁体   中英

AOP, Spring 4 MVC and @Around annotation

Today I'm trying to manage some AOP stuff with Spring 4 and I have a problem with @Around annotation. It works only after pointcut and behave like @After annotation. What is worse - combination @Before and @Around annotation effects only in calling a method after pointcut.

Combination @After and @Before works fine. To be honest - i have no idea why it works like that.

I also trying some mockito to detect calling AOP method but it's not working.

I have configuration class

@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = { "my.package.to.aop" })
public class AOPConfiguration {}

AOP class:

@Aspect
@Component
public class SmartLoggerAspect {

    @After("execution(* my.package.to.specific.function."
            + "repositories.PagingAndSortingBookRepository.findAll("
            + "org.springframework.data.domain.Pageable)  )")
    public void afterPage(JoinPoint joinPoint){
        System.out.println("\n\n\n\nCALLED AFTER: " + joinPoint.getSignature().getName());
    }

    @Before("execution(* my.package.to.specific.function."
            + "repositories.PagingAndSortingBookRepository.findAll("
            + "org.springframework.data.domain.Pageable)  )")
    public void beforePage(JoinPoint joinPoint){
        System.out.println("\n\n\n\nCALLED BEFORE: " + joinPoint.getSignature().getName());
    }

    @Around("execution(* my.package.to.specific.function."
            + "repositories.PagingAndSortingBookRepository.findAll("
            + "org.springframework.data.domain.Pageable)  )")
    public void aroundPage(JoinPoint joinPoint){
        System.out.println("\n\n\n\nCALLED AROUND: " +   joinPoint.getSignature().getName());
    }
}

And I made a unitTest for it

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = { JPAConfig.class, AOPConfiguration.class })
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class })
public class AspectTest {

    @Autowired
    PagingAndSortingBookRepository pagingAndSortingRepo;
    @Autowired
    SmartLoggerAspect smartLoggerAspect;

    JoinPoint joinPoint;


    @Test
    public void pagingTest(){
        pagingAndSortingRepo.findAll(new PageRequest(1, 1));
        //verify(smartLoggerAspect, times(1)).afterPage(joinPoint);
    }
}

I think the problem is using JoinPoint instead of ProceedindJoinPoint for the around advice method.

Also you need to invoke pjp.proceed method in the around advice.

Quoting from spring docs

The first parameter of the advice method must be of type ProceedingJoinPoint. Within the body of the advice, calling proceed() on the ProceedingJoinPoint causes the underlying method to execute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM