2016-10-11 12 views
0

今日、私はSpring 4でAOPを管理しようとしていますが、@Aroundアノテーションに問題があります。これは、ポイントカットの後でのみ動作し、@Afterアノテーションのように動作します。何が悪いですか?@Beforeと@Aroundアノテーションの組み合わせの組み合わせは、ポイントカット後にメソッドを呼び出す場合のみです。AOP、Spring 4 MVC、@Around annotation

組み合わせ@Afterと@Beforeは正常に動作します。正直言って、なぜそれがそのように機能するのか分かりません。

私はAOPメソッドの呼び出しを検出するためにいくつかのmockitoを試していますが、動作しません。

私はコンフィギュレーションクラス

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

をそして私は、私は問題はJoinPoint代わりのためのProceedindJoinPointを使用していると思わそれ

@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); 
    } 
} 
+0

「@ Before' + '@ After' __and__' @ Around'のアドバイスも必要なのはなぜですか? '@Around'のアドバイスでアドバイスを徹底的に試してみませんか? –

+0

私は初心者であり、AOPを使用する多くの方法を試しているからです。私がaBeforeとaAfter関数にコメントしてaroundだけを残しても、同じ問題が残っています。 –

+0

'Pointcut [...]の後でのみ動作します。 –

答えて

0

のためにユニットテストを作りましたaroundアドバイス方法。

また、周囲のアドバイスでpjp.proceedメソッドを呼び出す必要があります。

アドバイスメソッドの最初のパラメータは型ProceedingJoinPointでなければなりませんspring docs

から引用。アドバイス本体の中で、ProceedingJoinPointでproceed()を呼び出すと、基礎となるメソッドが実行されます。

+0

ありません私はこの機能を編集した @Around( "実行(* my.package.to.specific.function。" \t \t \t + "repositories.PagingAndSortingBookRepository.findAll(" \t \t \t +「org.springframework.data .domain.Pageable)) ") \t public void aroundPage(ProceedingJoinPoint proceedingJoinPoint)throws Throwable { \t \t proceedingJoinPoint.proceed(); \t \t \t \t System.out.println( "\ n \ n \ n \ n" + proceedingJoinPoint.getSignature()。getName()); \t} まだ同じ問題があります –

関連する問題