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);
}
}
Warum brauchen Sie auch '@ Before' +' @ After' __and__ '@ Around'? Warum versuchst du nicht, die Ratschläge in einem "@ Around" Rat zu finden? –
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 –
Was meinst du mit 'Es funktioniert nur nach pointcut [...]'? –