Search code examples
spring-bootspring-data-jpaaopspring-aop

Pointcut expression not matching the Spring Data method despite specfying exact name in expression


In my Spring Boot project, I have AddressRepository that brings all addresses from Database. I have an Aspect class and a pointcut expression that executes after the findAll() method called. When I execute my test case, the Advice is not being triggered and other methods like findAll(Sort sort), findAll(Pageable pageable) work just fine. I am not sure if this is a bug with Spring Boot or my expression. I tried with Spring Boot 2.0.5 and 2.1.0, nothing seemed to solve my problem

AddressLogging.java

@Aspect
@Configuration
public class AddressLogging {
    private Logger log=LoggerFactory.getLogger(AddressLogging.class);

    //@Pointcut("execution(* com.springtesting.repo.AddressRepository.*(..))")
    @Pointcut("execution(* com.springtesting.repo.AddressRepository.findAll())")
    public void getAddresses() {}

    @After("getAddresses()")
    public void afterAdvice() {
        log.error("Log Message: Inside afterAdvice() advice");
    }
}

AopTest.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class AopTest {
    @Autowired
    private AddressRepository addressRepository;

    @Test
    public void getAddresses() {
        //addressRepository.findAll(PageRequest.of(0,20, Sort.by("id")));
        addressRepository.findAll();
    }

    @Test
    public void findAddressById() {
        addressRepository.findById(1L);
    }
}

AddressRepository

public interface AddressRepository extends JpaRepository<Address,Long> {}

Solution

  • A Spring AOP aspect should also be a @Component and be picked up by component scan. I have no idea why you added @Configuration to the aspect instead because there is no configuration here.

    Maybe your test of a separate configuration class should bear the @Configuration annotation and you should also activate something like @EnableAspectJAutoProxy(proxyTargetClass = true) and @ComponentScan(basePackages = { "de.scrum_master" }).

    Here is a snippet from one of my Spring AOP playground projects (I hardly use it, I don't use Spring AOP or even Spring itself, usually I use the more powerful AspectJ:

    package de.scrum_master.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    
    @Configuration
    @EnableAspectJAutoProxy(proxyTargetClass = true)
    @ComponentScan(basePackages = { "de.scrum_master" })
    public class Application2 {
      public static void main(String[] args) throws Exception {
        ApplicationContext appContext = new AnnotationConfigApplicationContext(Application2.class);
        B b = (B) appContext.getBean("b");
        System.out.println(b.getData("bbb"));
        A a = (A) appContext.getBean("b");
        System.out.println(a.getData("aaa"));
      }
    }