1.選擇哪種AOP
(1) 使用Spring AOP比使用完整版的AspectJ更方便簡單,因為不需要在開發(fā)和構建過程中引入AspectJ編譯器以及織入器,如果我們只希望通知能夠在Spring Bean上執(zhí)行,那么選用Spring AOP就可以了,如果我們希望通知能夠在不由Spring所管理的對象上執(zhí)行,那么就需要使用AspectJ,如果我們希望為除方法以外的連接點(比如成員變量)提供通知,那么也需要使用AspectJ
2.Spring AOP的代理機制
(1) Spring AOP使用Jdk動態(tài)代理或Cglib動態(tài)代理來為目標對象創(chuàng)建代理對象,Jdk動態(tài)代理由Jdk提供,而Cglib動態(tài)代理則是由一個開源類庫提供,如果要代理的目標對象至少實現(xiàn)了一個接口,那么就會使用Jdk動態(tài)代理,否則如果目標對象沒有實現(xiàn)任何接口,那么就會使用Cglib動態(tài)代理,創(chuàng)建一個Cglib代理對象
(2) Spring默認使用Jdk動態(tài)代理,但我們可以強制讓Spring始終使用Cglib動態(tài)代理,但需注意,使用Cglib動態(tài)代理,無法對final修飾的方法織入通知,因為這些方法不能在子類中被重寫,具體開啟Cglib動態(tài)代理的方式如下
<!-- 方式一:在使用基于xml的配置時,設置<aop:config/>標簽中的proxy-target-class屬性為true -->
<aop:config proxy-target-class="true">
<!-- ... -->
</aop:config>
<!-- 方式二:在混合使用基于xml和注解的配置時,設置<aop:aspectj-autoproxy/>標簽中的proxy-target-class屬性為true -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 方式三:在使用基于注解的配置時,設置@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指針進行自調用不會觸發(fā)通知的執(zhí)行
//一個普通的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 = "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();
}
//接著,再執(zhí)行打印,可見此時通知已被正確執(zhí)行
beforePrint...
a...
beforePrint...
b...
Spring不推薦使用如上的方法,因為這會使Spring AOP與我們的代碼強耦合,具有侵入性,最好的方式是重構我們的代碼,避免發(fā)生自調用,此外,Spring AOP會產(chǎn)生這種問題的原因是Spring AOP是基于代理實現(xiàn)的,而AspectJ框架就不存在這種自調用問題,因為它不是一個基于代理的AOP框架文章來源:http://www.zghlxwxcb.cn/news/detail-437496.html
3.基于java代碼的AOP文章來源地址http://www.zghlxwxcb.cn/news/detail-437496.html
public class ExampleA{
public void a() {
System.out.println("a...");
}
}
//切面必須由@Aspect注解標注,否則容器會拋出異常
@Aspect
public class Logger {
@Before(value = "execution(* cn.example.spring.boke.ExampleA.*(..))")
public void beforePrint() {
System.out.println("beforePrint...");
}
}
//創(chuàng)建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();
}
到了這里,關于Spring AOP官方文檔學習筆記(四)之Spring AOP的其他知識點的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!