gpt4 book ai didi

java - 围绕 Spring-AOP 切入点和继承的澄清

转载 作者:行者123 更新时间:2023-11-30 09:44:42 24 4
gpt4 key购买 nike

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

关于java - 围绕 Spring-AOP 切入点和继承的澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7727454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com