简体   繁体   English

AOP,Spring 4 MVC和@Around批注

[英]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. 今天,我试图用Spring 4管理一些AOP东西,而@Around注释存在问题。 It works only after pointcut and behave like @After annotation. 它仅在切入点后起作用,并且行为类似于@After注释。 What is worse - combination @Before and @Around annotation effects only in calling a method after pointcut. 更糟糕的是,@Before和@Around注释组合只能在切入点后调用方法时起作用。

Combination @After and @Before works fine. @After和@Before组合可以正常工作。 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. 我还尝试了一些模仿来检测调用AOP方法,但是它不起作用。

I have configuration class 我有配置课

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

AOP class: AOP类:

@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 我为此做了一个unitTest

@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. 我认为问题是在around建议方法中使用JoinPoint而不是ProceedindJoinPoint

Also you need to invoke pjp.proceed method in the around advice. 另外,您还需要在around建议中调用pjp.proceed方法。

Quoting from spring docs 引用春季文档

The first parameter of the advice method must be of type ProceedingJoinPoint. 咨询方法的第一个参数必须是ProceedingJoinPoint类型。 Within the body of the advice, calling proceed() on the ProceedingJoinPoint causes the underlying method to execute. 在建议的正文中,在ProceedingJoinPoint上调用proce()会使基础方法执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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