java-关于Spring-AOP切入点和继承的说明

给定my.package中的以下示例类…

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

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

以及以下Spring-AOP切入点…

<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.*(..))" />

对Bar实例的上述切入点应用建议的结果是什么?尤其是…

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

我想我缺少切入点如何与继承交互的一些基本真理,因此,对幕后的解释/文档可能会走很长一段路.

解决方法:

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() { ... }
  }
上一篇:弹簧-@Configurable不适用于在@PostConstruct方法中初始化的对象


下一篇:春天-类型化/泛型方面值得努力吗?