Announcement Announcement Module
No announcement yet.
How to inject caller into advice? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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:
    <aop:pointcut id="pointcutId" expression="execution(* ClassB.methodB(..))"/>
    Would changing it to this inject the calling class into the advice?
    <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
    That is difficult with Spring AOP. If you use AspectJ weaving (where 'call' join point is available) you could do something like:
    pointcut callToB(ClassB sb) : call(* ClassB.methodB(..)) && target(cb);


    • #3
      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!