关于Spring-AOP切入点和继承的说明 [英] Clarification around Spring-AOP pointcuts and inheritance

查看:246
本文介绍了关于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切入点...

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

对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.

推荐答案

来自

匹配方法执行连接点时,如果执行切入点 方法签名指定了声明类型,切入点将仅 匹配以该类型声明的方法,或覆盖方法的方法 在该类型中声明或继承的类型.所以切入点

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

执行(公共无效中间.*())

提取所有方法执行 对于返回void并且没有参数的公共方法 在Middle中声明或被Middle继承,即使这些方法是 在Middle的子类中重写.因此,切入点将挑选出 此代码中Sub.m()的方法执行连接点:

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() { ... }
  }

这篇关于关于Spring-AOP切入点和继承的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆