Announcement Announcement Module
Collapse
No announcement yet.
Trying to advise a method from Spring Data JPA repository using aspectJ Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trying to advise a method from Spring Data JPA repository using aspectJ

    Hello,

    I am trying to define a pointcut whose advice will run when method from a Spring Data JPA repository is invoked.

    Here is my anonymous pointcut together with the inline advice (from PliEventManagerAspect):
    Code:
    after(Pli pli) returning: (execution(* org.springframework.data.repository.CrudRepository+.save(Pli)) && args(pli)){
        System.out.println("Caught!!!!");   
    }
    Here the definition for my PliRepository:
    Code:
    public interface PliRepository extends GlobalRepository<Pli, Long>, PliRepositoryCustom {
    and PliRepositoryImpl:
    Code:
    public class PliRepositoryImpl extends QueryDslRepositorySupport implements PliRepositoryCustom {
    and PliRepositoryCustom:
    Code:
    public interface PliRepositoryCustom {
    and lastly GlobalRepository:
    Code:
    @NoRepositoryBean
    public interface GlobalRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
    I have also set the javaagent command line arg. And I have the following aop.xml:
    Code:
    <!DOCTYPE aspectj PUBLIC
    "-//AspectJ//DTD//EN"
    "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
    <aspectj>
        <weaver options="-verbose">
            <include within="org.springframework.data.repository..*"/>
        </weaver>
        <aspects>
            <aspect name="trc.suivi.aspects.PliEventManagerAspect" />
        </aspects>
    </aspectj>
    The above advice, which is supposed to run in LTW since it is advising a class in a jar, is not run at all... I am sure I must make some mistake in the pointcut definition. Can anyone please help?

    Regards,

    Julien.

    P.S. I deliberately posted on this specific forum instead of the AOP one because my question requires knowledge of Spring Data JPA and especially I can't pinpoint the specific class I need to specify in my poincut...

  • #2
    For starters your aspect will only be applied to beans in the package org.springframework.data.repository (and subpackages)... I doubt your repositories reside in that package...

    Also not sure why you want to use LTW as long as the bean is a spring bean you can simply use Spring AOP...

    Comment


    • #3
      Hi Marten,
      The trouble is that I want to advise the save method and it is not defined on my repository but rather on the CrudRepository. Isn't it?
      Julien.

      Comment


      • #4
        The CrudRepository is an interface which only contains the definition. You can only advice classes not interfaces (would be pretty useless)... And the class you want to advice isn't in the package you want advices (the include in your aop.xml)...

        Comment


        • #5
          You can only advice classes not interfaces
          I see...

          The thing is I have already tried advising my PliRepositoryImpl class and it does not work. More importantly I don't know on which class the save method is defined...

          Can you help?

          Comment


          • #6
            Remove the include so that all your classes are scanned...

            Comment


            • #7
              As I am not sure where the save method is defined I wrote my pointcut/advice as follows:
              Code:
              after(Pli pli) returning: (execution (* *.save(Pli)) && args(pli)){
              		System.out.println("caught!!!!");	
              }
              and Eclipse/STS warns me that
              advice defined in trc.suivi.aspects.PliEventManagerAspect has not been applied

              Comment


              • #8
                Please read... It wasn't about your pointcut it was your aop.xml...

                Your pointcut was mentioning all implementations of the CrudRepository...

                I would make a minor change however... (I also wonder why the classic aspect style and not @Aspect?).

                Code:
                after(Pli pli) returning: (execution(* org.springframework.data.repository.CrudRepository+.save(*)) && args(pli)){
                    System.out.println("Caught!!!!");   
                }

                Comment


                • #9
                  Thanks a lot Marten.
                  That fixed it.
                  Julien.

                  Comment

                  Working...
                  X