Results 1 to 9 of 9

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

  1. #1
    Join Date
    Nov 2007
    Posts
    177

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Nov 2007
    Posts
    177

    Default

    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.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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)...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Nov 2007
    Posts
    177

    Default

    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?

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Remove the include so that all your classes are scanned...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Nov 2007
    Posts
    177

    Default

    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

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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!!!!");   
    }
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Nov 2007
    Posts
    177

    Default

    Thanks a lot Marten.
    That fixed it.
    Julien.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •