代理機制
Spring AOP 使用 JDK 動態代理或 CGLIB 為給定的目標物件建立代理。JDK 動態代理內置於 JDK 中,而 CGLIB 是一種常見的開源類定義庫(已重新打包到 spring-core 中)。
如果待代理的目標物件實現至少一個介面,則使用 JDK 動態代理,並且代理目標型別實現的所有介面。如果目標物件不實現任何介面,則建立 CGLIB 代理,它是目標型別的執行時生成子類。
如果你想強制使用 CGLIB 代理(例如,代理為目標物件定義的每個方法,而不僅僅是其介面實現的方法),你可以這樣做。但是,你應該考慮以下問題:
-
final類不能被代理,因為它們不能被擴充套件。 -
final方法不能被增強,因為它們不能被重寫。 -
private方法不能被增強,因為它們不能被重寫。 -
不可見的方法——例如,來自不同包的父類中的包私有方法——不能被增強,因為它們實際上是私有的。
-
你的代理物件的建構函式不會被呼叫兩次,因為 CGLIB 代理例項是透過 Objenesis 建立的。但是,如果你的 JVM 不允許繞過建構函式,你可能會看到雙重呼叫以及 Spring AOP 支援中相應的除錯日誌條目。
-
你的 CGLIB 代理用法可能會面臨 Java 模組系統的限制。作為典型情況,當部署在模組路徑上時,你無法為
java.lang包中的類建立 CGLIB 代理。這種情況需要 JVM 引導標誌--add-opens=java.base/java.lang=ALL-UNNAMED,該標誌不適用於模組。
強制使用特定的 AOP 代理型別
要強制使用 CGLIB 代理,請將 <aop:config> 元素的 proxy-target-class 屬性的值設定為 true,如下所示:
<aop:config proxy-target-class="true">
<!-- other beans defined here... -->
</aop:config>
當使用 @AspectJ 自動代理支援時,要強制使用 CGLIB 代理,請將 <aop:aspectj-autoproxy> 元素的 proxy-target-class 屬性設定為 true,如下所示:
<aop:aspectj-autoproxy proxy-target-class="true"/>
|
多個 需要明確的是,在 |
@EnableAspectJAutoProxy、@EnableTransactionManagement 和相關的配置註解提供了相應的 proxyTargetClass 屬性。這些也會合併為一個統一的自動代理建立器,從而在執行時有效地應用最強的代理設定。從 7.0 版本開始,這同樣適用於單獨的代理處理器,例如 @EnableAsync,它們始終參與給定應用程式中所有自動代理嘗試的統一全域性預設設定。
全域性預設代理型別可能因設定而異。雖然核心框架預設建議使用基於介面的代理,但 Spring Boot 可能會根據配置屬性預設啟用基於類的代理。
從 7.0 版本開始,透過在給定 @Bean 方法或 @Component 類上使用 @Proxyable 註解,可以為單個 bean 強制指定特定的代理型別,其中 @Proxyable(INTERFACES) 或 @Proxyable(TARGET_CLASS) 會覆蓋任何全域性配置的預設值。對於非常特定的目的,你甚至可以透過 @Proxyable(interfaces=…) 指定要使用的代理介面,從而將暴露限制為選定的介面,而不是目標 bean 實現的所有介面。
理解 AOP 代理
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 編譯時織入和載入時織入沒有這個自呼叫問題,因為它們是在位元組碼內部而不是透過代理應用通知的。 |