Results 1 to 3 of 3

Thread: How to inject caller into advice?

  1. #1
    Join Date
    May 2005
    Posts
    17

    Default How to inject caller into advice?

    Hello all, I'm new to AOP and I've followed several of the examples and read the docs, but I may not be grasping something fully. Is there a way to inject the caller of a pointcut method into the aspect? For example suppose ClassA.methodA calls ClassB.methodB and methodB is defined as a pointcut (in which case the InterceptorA.doSomething method is defined). Can ClassA (in my understanding, the "caller") get injected into the aspect so I can interrogate some portions or pull attributes out of it in the advice?

    FWIW, I'm using the schema approach for context definition and I would have something like this currently:
    Code:
    <aop:pointcut id="pointcutId" expression="execution(* ClassB.methodB(..))"/>
    Would changing it to this inject the calling class into the advice?
    Code:
    <aop:pointcut id="pointcutId" expression="execution(* ClassB.methodB(..)) and bean(ClassA)"/>
    If so, would it inject the instance of the bean that triggered the pointcut?

    Thanks in advance!

  2. #2
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    That is difficult with Spring AOP. If you use AspectJ weaving (where 'call' join point is available) you could do something like:
    Code:
    pointcut callToB(ClassB sb) : call(* ClassB.methodB(..)) && target(cb);
    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  3. #3
    Join Date
    May 2005
    Posts
    17

    Default

    That's what I was afraid of. Looks like I'll have to refactor all my pointcut enabled classes to extend an abstact class so I can set the values needed in the aspect so I can retrieve them in the called methods. Something I was trying to avoid, but doable.

    Thanks for the information!

Posting Permissions

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