2016-10-11 2 views
0

Heute versuche ich einige AOP Sachen mit Spring 4 zu verwalten und ich habe ein Problem mit @Around Annotation. Es funktioniert nur nach Pointcut und verhält sich wie @ After Annotation. Was ist schlimmer - Kombination @Before und @Around Annotation Effekte nur beim Aufruf einer Methode nach Pointcut.AOP, Spring 4 MVC und @Around Annotation

Kombination @After und @Before funktioniert gut. Um ehrlich zu sein - ich habe keine Ahnung, warum es so funktioniert.

Ich versuche auch einige Mockito zu erkennen aufrufende AOP-Methode, aber es funktioniert nicht.

Ich habe Konfigurationsklasse

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

AOP Klasse:

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

Und ich machte einen Unittest für sie

@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

Warum brauchen Sie auch '@ Before' +' @ After' __and__ '@ Around'? Warum versuchst du nicht, die Ratschläge in einem "@ Around" Rat zu finden? –

+0

Weil ich ein Anfänger bin und viele Möglichkeiten versuche, AOP zu benutzen. Wenn ich aBefore und aAfter following and leave aAround nur, immer noch habe ich das gleiche Problem –

+0

Was meinst du mit 'Es funktioniert nur nach pointcut [...]'? –

Antwort

0

denke ich, das Problem JoinPoint wird anstelle von ProceedindJoinPoint für die around beratungsmethode.

Auch müssen Sie pjp.proceed Methode in der Umgebung Rat aufrufen.

von spring docs

Der erste Parameter der Beratung Methode muss vom Typ ProceedingJoinPoint sein Zitiert. Der Aufruf von proceed() am ProceedingJoinPoint bewirkt, dass die zugrunde liegende Methode ausgeführt wird.

+0

No i bearbeitet diese Funktion @Around ("execution (* 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) wirft Throwable { \t \t proceedingJoinPoint.proceed(); \t \t \t System.out.println ("\ n \ n \ n \ nZUORDNET:" + progressingJoinPoint.getSignature(). GetName()); \t} Und immer noch das gleiche Problem –