开发者

aspect for internal method call

开发者 https://www.devze.com 2023-03-01 06:27 出处:网络
I have a requirement where in I need to place an aspect around an internal method call, by internal I mean

I have a requirement where in I need to place an aspect around an internal method call, by internal I mean

class Person{ 
        public void outerMethod()
          {
            internalMethod() 
          } 
          // Need an aspect here !!
          public void internalMethod() 
          {
           }
}

I am aware that this is not possible using convention开发者_如何学JAVAal spring aop, does native aspectj provide a facility for this ? and can I have pointers for the same


Sure thing:

// match the call in outerMethod
pointcut callInnerMethod() : call(* Person.internalMethod(..));

Or

// match innerMethod itself
pointcut executeInnerMethod() : execution(* Person.internalMethod(..));

You can combine either of these with before, after or around advices:

void before() : callInnerMethod() /* or executeInnerMethod() */ {
    // do something here
}

void around() : callInnerMethod() /* or executeInnerMethod() */{
    // do something here
    proceed();
    // do something else here
}

void after() returning() : callInnerMethod() /* or executeInnerMethod() */{
    // do something here
}

Note this is traditional aspectj-Syntax, the one you use in .aj files. You can also use all of these constructs with different Syntax in @AspectJ .java syntax.

Please consult the AspectJ Quick Reference or read AspectJ in Action


I'll just add an answer to my own question.

The most important thing is this cannot be achieved through spring aop,

It can be achieved by aspectJ , when I tried out loadtime weaving of aspectj it did not seem to provide a solution. (This is not completely researched) ... What i found was that it placed as aspect around the object .. so all calls made through the object invoked the aspect, however internal calls did not invoke the aspect

Only compile time weaving can resolve this.

the same question is asked here too

Thanks Sudarshan


Reference to this documentation and use execution or call pointcuts and around advice. As far as you described your task they perfectly suit your needs.

0

精彩评论

暂无评论...
验证码 换一张
取 消