Clarification around Spring-AOP pointcuts and inhe

2020-04-14 09:02发布

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:pointcut 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.

1条回答
叛逆
2楼-- · 2020-04-14 09:15

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() { ... }
  }
查看更多
登录 后发表回答