[英]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.