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












0














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> {}









share|improve this question





























    0














    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> {}









    share|improve this question



























      0












      0








      0


      0





      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> {}









      share|improve this question















      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> {}






      spring-boot spring-data-jpa aop spring-aop






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 11 at 3:41









      kriegaex

      30.6k36398




      30.6k36398










      asked Nov 20 at 19:00









      Jadda

      212114




      212114
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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"));
          }
          }





          share|improve this answer























          • Thanks, kriegaex. Adding @Component to Aspect class solved my issue
            – Jadda
            Dec 14 at 20:19











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399810%2fpointcut-expression-not-matching-the-spring-data-method-despite-specfying-exact%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          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"));
          }
          }





          share|improve this answer























          • Thanks, kriegaex. Adding @Component to Aspect class solved my issue
            – Jadda
            Dec 14 at 20:19
















          1














          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"));
          }
          }





          share|improve this answer























          • Thanks, kriegaex. Adding @Component to Aspect class solved my issue
            – Jadda
            Dec 14 at 20:19














          1












          1








          1






          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"));
          }
          }





          share|improve this answer














          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"));
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 15 at 1:12

























          answered Dec 11 at 5:03









          kriegaex

          30.6k36398




          30.6k36398












          • Thanks, kriegaex. Adding @Component to Aspect class solved my issue
            – Jadda
            Dec 14 at 20:19


















          • Thanks, kriegaex. Adding @Component to Aspect class solved my issue
            – Jadda
            Dec 14 at 20:19
















          Thanks, kriegaex. Adding @Component to Aspect class solved my issue
          – Jadda
          Dec 14 at 20:19




          Thanks, kriegaex. Adding @Component to Aspect class solved my issue
          – Jadda
          Dec 14 at 20:19


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399810%2fpointcut-expression-not-matching-the-spring-data-method-despite-specfying-exact%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen