1.選擇哪種AOP
(1) 使用Spring AOP比使用完整版的AspectJ更方便簡單,因為不需要在開發和構建程序中引入AspectJ編譯器以及織入器,如果我們只希望通知能夠在Spring Bean上執行,那么選用Spring AOP就可以了,如果我們希望通知能夠在不由Spring所管理的物件上執行,那么就需要使用AspectJ,如果我們希望為除方法以外的連接點(比如成員變數)提供通知,那么也需要使用AspectJ
2.Spring AOP的代理機制
(1) Spring AOP使用Jdk動態代理或Cglib動態代理來為目標物件創建代理物件,Jdk動態代理由Jdk提供,而Cglib動態代理則是由一個開源類別庫提供,如果要代理的目標物件至少實作了一個介面,那么就會使用Jdk動態代理,否則如果目標物件沒有實作任何介面,那么就會使用Cglib動態代理,創建一個Cglib代理物件
(2) Spring默認使用Jdk動態代理,但我們可以強制讓Spring始終使用Cglib動態代理,但需注意,使用Cglib動態代理,無法對final修飾的方法織入通知,因為這些方法不能在子類中被重寫,具體開啟Cglib動態代理的方式如下
<!-- 方式一:在使用基于xml的配置時,設定<aop:config/>標簽中的proxy-target-class屬性為true -->
<aop:config proxy-target->
<!-- ... -->
</aop:config>
<!-- 方式二:在混合使用基于xml和注解的配置時,設定<aop:aspectj-autoproxy/>標簽中的proxy-target-class屬性為true -->
<aop:aspectj-autoproxy proxy-target-/>
<!-- 方式三:在使用基于注解的配置時,設定@EnableAspectJAutoProxy注解中的proxyTargetClass屬性為true -->
@EnableAspectJAutoProxy(proxyTargetClass = true)
(3) 當一個bean被代理后,我們從容器中獲取到這個bean,并對其使用 .getClass().getName() 方法來輸出它的類名稱,可見如 cn.example.spring.boke.ExampleA$$EnhancerBySpringCGLIB$$ff6c22d2或com.sun.proxy.$Proxy18 這樣的輸出,而當我們關閉掉AOP后,得到的通常是形如 cn.example.spring.boke.ExampleA 這樣的輸出,這其實是因為我們從容器中獲取的是該bean被增強過后的代理物件,而非它原始的目標物件,因而,對這個bean的方法呼叫就是對代理物件的方法呼叫,然后由代理物件委托呼叫原始物件上相關的方法以及該方法相關的攔截器(advice),如下

(4) 在目標物件中,使用this指標進行自呼叫不會觸發通知的執行
//一個普通的bean,在它的a方法中使用this指標,自呼叫b方法
@Component
public class ExampleA{
public void a() {
System.out.println("a...");
this.b();
}
public void b() {
System.out.println("b...");
}
}
//切面,切入ExampleA這個bean中的所有方法
@Component
@Aspect
public class Logger {
@Before(value = "https://www.cnblogs.com/shame11/archive/2023/05/09/execution(* cn.example.spring.boke.ExampleA.*(..))")
public void beforePrint() {
System.out.println("beforePrint...");
}
}
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "cn.example.spring.boke")
public class Config { }
//獲取ExampleA,呼叫a方法,列印結果
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
ExampleA exampleA = ctx.getBean(ExampleA.class);
exampleA.a();
//可見,Spring對a方法進行了織入,而b方法卻沒有,原因就是因為這里的this指向的是目標物件,一個普通的bean ExampleA,而非它的代理物件,自然而然無法進行織入了,因此關鍵的目標就是如何獲取到代理物件
beforePrint...
a...
b...
//想要獲取代理物件,首先要先將@EnableAspectJAutoProxy注解中的exposeProxy屬性設定為true
@EnableAspectJAutoProxy(exposeProxy = true)
//接著修改ExampleA中的a方法,在呼叫b方法時不再使用this指標,而是AopContext.currentProxy(),即獲取當前物件的代理物件
public void a() {
System.out.println("a...");
// this.b();
((ExampleA) AopContext.currentProxy()).b();
}
//接著,再執行列印,可見此時通知已被正確執行
beforePrint...
a...
beforePrint...
b...
Spring不推薦使用如上的方法,因為這會使Spring AOP與我們的代碼強耦合,具有侵入性,最好的方式是重構我們的代碼,避免發生自呼叫,此外,Spring AOP會產生這種問題的原因是Spring AOP是基于代理實作的,而AspectJ框架就不存在這種自呼叫問題,因為它不是一個基于代理的AOP框架
3.基于java代碼的AOP
public class ExampleA{
public void a() {
System.out.println("a...");
}
}
//切面必須由@Aspect注解標注,否則容器會拋出例外
@Aspect
public class Logger {
@Before(value = "https://www.cnblogs.com/shame11/archive/2023/05/09/execution(* cn.example.spring.boke.ExampleA.*(..))")
public void beforePrint() {
System.out.println("beforePrint...");
}
}
//創建AOP工廠,生成代理物件
public static void main(String[] args) throws Exception {
//1.使用AspectJProxyFactory工廠,用于生成目標物件的代理物件
AspectJProxyFactory factory = new AspectJProxyFactory(new ExampleA());
//2.添加一個切面,該切面必須由@Aspect注解標注
factory.addAspect(Logger.class);
//3.生成代理物件
ExampleA proxy = factory.getProxy();
proxy.a();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/551987.html
標籤:其他
下一篇:返回列表