繁体   English   中英

AOP,Spring 4 MVC和@Around批注

[英]AOP, Spring 4 MVC and @Around annotation

今天,我试图用Spring 4管理一些AOP东西,而@Around注释存在问题。 它仅在切入点后起作用,并且行为类似于@After注释。 更糟糕的是,@Before和@Around注释组合只能在切入点后调用方法时起作用。

@After和@Before组合可以正常工作。 老实说-我不知道为什么它会那样工作。

我还尝试了一些模仿来检测调用AOP方法,但是它不起作用。

我有配置课

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

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());
    }
}

我为此做了一个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);
    }
}

我认为问题是在around建议方法中使用JoinPoint而不是ProceedindJoinPoint

另外,您还需要在around建议中调用pjp.proceed方法。

引用春季文档

咨询方法的第一个参数必须是ProceedingJoinPoint类型。 在建议的正文中,在ProceedingJoinPoint上调用proce()会使基础方法执行。

暂无
暂无

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

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