开发者

Clarification around Spring-AOP pointcuts and inheritance

Given the following example classes in my.package...

public class Foo {
    public void logicNotInBar()     {/*code*/}
    public void logicBarOverrides() {/*code*/}
}

public class Bar extends Foo {
    public void logicBarOverrides() {/*code*/}
}

and the following Spring-AOP pointcuts...

<aop:poi开发者_C百科ntcut id="myPointcutAll" expression="execution(* my.package.*.*(..))"   />
<aop:pointcut id="myPointcutFoo" expression="execution(* my.package.Foo.*(..))" />
<aop:pointcut id="myPointcutBar" expression="execution(* my.package.Bar.*(..))" />

What is the result of advice applied to the above pointcuts on instances of Bar? In particular...

Bar bar = new Bar();
bar.logicNotInBar();      // will myPointcutBar advice trigger?
bar.logicBarOverrides();  // is myPointcutFoo ignored here?

I think I am missing some basic truth of how pointcuts interact with inheritance so an under-the-hood explanation/doc would probably go a long way.


From aspectj documentation:

When matching method-execution join points, if the execution pointcut method signature specifies a declaring type, the pointcut will only match methods declared in that type, or methods that override methods declared in or inherited by that type. So the pointcut

execution(public void Middle.*())

picks out all method executions for public methods returning void and having no arguments that are either declared in, or inherited by, Middle, even if those methods are overridden in a subclass of Middle. So the pointcut would pick out the method-execution join point for Sub.m() in this code:

  class Super {
    protected void m() { ... }
  }
  class Middle extends Super {
  }
  class Sub extends Middle {
    public void m() { ... }
  }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜