Spring 框架

Spring 框架

Spring AOP 是基于代理的。在你编写自己的切面或使用 Spring Framework 提供的任何基于 Spring AOP 的切面之前,理解上面这句话的语义至关重要。

首先考虑以下代码片段所示的普通、未代理的对象引用场景:

Java

Kotlin

public class SimplePojo implements Pojo {

public void foo() {

// this next method invocation is a direct call on the 'this' reference

this.bar();

}

public void bar() {

// some logic...

}

}

class SimplePojo : Pojo {

fun foo() {

// this next method invocation is a direct call on the 'this' reference

this.bar()

}

fun bar() {

// some logic...

}

}

如果你在对象引用上调用方法,该方法将直接在该对象引用上调用,如下图和列表所示:

Java

Kotlin

public class Main {

public static void main(String[] args) {

Pojo pojo = new SimplePojo();

// this is a direct method call on the 'pojo' reference

pojo.foo();

}

}

fun main() {

val pojo = SimplePojo()

// this is a direct method call on the 'pojo' reference

pojo.foo()

}

当客户端代码拥有的引用是代理时,情况略有不同。考虑以下图表和代码片段:

Java

Kotlin

public class Main {

public static void main(String[] args) {

ProxyFactory factory = new ProxyFactory(new SimplePojo());

factory.addInterface(Pojo.class);

factory.addAdvice(new RetryAdvice());

Pojo pojo = (Pojo) factory.getProxy();

// this is a method call on the proxy!

pojo.foo();

}

}

fun main() {

val factory = ProxyFactory(SimplePojo())

factory.addInterface(Pojo::class.java)

factory.addAdvice(RetryAdvice())

val pojo = factory.proxy as Pojo

// this is a method call on the proxy!

pojo.foo()

}

这里的关键是,Main 类的 main(..) 方法中的客户端代码拥有对代理的引用。这意味着对该对象引用的方法调用是对代理的调用。因此,代理可以将调用委托给与该特定方法调用相关的所有拦截器(通知)。然而,一旦调用最终到达目标对象(本例中是 SimplePojo 引用),它可能对自己进行的任何方法调用,例如 this.bar() 或 this.foo(),都将针对 this 引用而不是代理进行调用。这具有重要的影响。这意味着自调用不会导致与方法调用相关的通知有机会运行。换句话说,通过显式或隐式 this 引用进行的自调用将绕过通知。

为了解决这个问题,你有以下选择。

避免自调用

最好的方法(“最好”一词在此处并非严格意义)是重构你的代码,使自调用不会发生。这确实需要你付出一些努力,但它是最好、侵入性最小的方法。

注入自引用

另一种方法是利用自注入,并通过自引用而不是通过 this 调用代理上的方法。

使用 AopContext.currentProxy()

最后这种方法非常不鼓励,我们犹豫是否指出它,而更倾向于前面的选项。然而,作为最后的手段,你可以选择将类中的逻辑绑定到 Spring AOP,如下例所示。

Java

Kotlin

public class SimplePojo implements Pojo {

public void foo() {

// This works, but it should be avoided if possible.

((Pojo) AopContext.currentProxy()).bar();

}

public void bar() {

// some logic...

}

}

class SimplePojo : Pojo {

fun foo() {

// This works, but it should be avoided if possible.

(AopContext.currentProxy() as Pojo).bar()

}

fun bar() {

// some logic...

}

}

使用 AopContext.currentProxy() 会使你的代码与 Spring AOP 完全耦合,并且使类本身意识到它正在 AOP 上下文中使用,这会降低 AOP 的一些好处。它还需要将 ProxyFactory 配置为暴露代理,如下例所示:

Java

Kotlin

public class Main {

public static void main(String[] args) {

ProxyFactory factory = new ProxyFactory(new SimplePojo());

factory.addInterface(Pojo.class);

factory.addAdvice(new RetryAdvice());

factory.setExposeProxy(true);

Pojo pojo = (Pojo) factory.getProxy();

// this is a method call on the proxy!

pojo.foo();

}

}

fun main() {

val factory = ProxyFactory(SimplePojo())

factory.addInterface(Pojo::class.java)

factory.addAdvice(RetryAdvice())

factory.isExposeProxy = true

val pojo = factory.proxy as Pojo

// this is a method call on the proxy!

pojo.foo()

}

AspectJ 编译时织入和加载时织入没有这个自调用问题,因为它们是在字节码内部而不是通过代理应用通知的。

相关推荐

十大好听的校园广播歌曲 适合学校广播的歌曲 校园广播站歌曲100首→MAIGOO生活榜
处理各种平台不给出的问题,威:80170160
bat365手机版app

处理各种平台不给出的问题,威:80170160

📅 10-03 👀 2626
深耕河南 布局新事业 富士康向“新”再出发
bat365手机版app

深耕河南 布局新事业 富士康向“新”再出发

📅 09-01 👀 7857