Spring AOP 實現原理(四)特性解讀

AspectJ簡介

咱們花了不少篇幅講解了Spring AOP的實現原理動態代理,可是隻有動態代理是不夠的,好比說前面提到的Aspect、Join Point、Pointcut等,這些有關AOP的抽象概念也很是重要,咱們知道這部分其實是由AspectJ提供的,那麼AspectJ是什麼呢?html

aspectj isgit

  • a seamless aspect-oriented extension to the Javatm programming language
  • Java platform compatible
  • easy to learn and use

AspectJ都包括了哪些內容?github

  • 提供了面向切面的模型,像aspect、pointcut等
  • 有本身的一套完整的語言體系,相似於execution(** com.aop.biz..(..)) 表達式這種
  • 擁有本身的一套開發編譯工具
  • 本質上也是經過訪問字節碼現的AOP

或許有人要問,既然AspectJ是須要編譯的,那麼Spring AOP怎麼沒有使用額外的編譯工具呢?spring

那是由於Spring僅使用了AspectJ的模型和語言表達式部分。app

AspectJ的相關內容很是多,幾篇文章都不必定能說的完,這塊不是咱們討論的重點,有興趣的同窗請參考附錄資料。框架

Spring AOP官方文檔解讀

在解讀源碼以前,咱們先看下Spring AOP的官方文檔,從中咱們能獲取到極爲重要的信息。less

Spring provides simple and powerful ways of writing custom aspects by using either a schema-based approach or the @AspectJ annotation style. Both of these styles offer fully typed advice and use of the AspectJ pointcut language while still using Spring AOP for weaving.

Spring 提供了schema和@AspectJ註解兩種風格,這兩種風格本質上使用的都是AspectJ提供的切入點語言。eclipse

Weaving: linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime

在Spring AOP的織入特性中,咱們得知Spring AOP僅支持runtime織入,而AspectJ支持compile time,load time,runtime三種.ide

Spring AOP Capabilities and Goals

Spring AOP currently supports only method execution join points (advising the execution of methods on Spring beans). Field interception is not implemented, although support for field interception could be added without breaking the core Spring AOP APIs. If you need to advise field access and update join points, consider a language such as AspectJ.工具

Spring AOP never strives to compete with AspectJ to provide a comprehensive AOP solution. We believe that both proxy-based frameworks such as Spring AOP and full-blown frameworks such as AspectJ are valuable and that they are complementary, rather than in competition.

上面這段內容主要提到了,Spring AOP僅支持method接入點,不支持field接入點;Spring AOP的目標歷來都不是提供完整的AOP解決方案,Spring AOP和AspectJ更像是互補的關係,而不是競爭關係.

AOP Porxies

Spring AOP defaults to using standard JDK dynamic proxies for AOP proxies. This enables any interface (or set of interfaces) to be proxied. Spring AOP can also use CGLIB proxies. This is necessary to proxy classes rather than interfaces. By default, CGLIB is used if a business object does not implement an interface

Spring AOP默認使用JDK動態代理,同時也支持CGLIB,當代理類而不是接口的時候,默認使用CGLIB方式.

從官方文檔中咱們可以得出來如下重要信息:

  • Spring AOP是一個純Java的AOP框架
  • Spring AOP是基於代理(proxy-based)的AOP框架
  • Spring AOP並非一個完整的AOP實現,它主要是提供了AOP和IoC之間的無縫集成
  • Spring AOP的pointcut採用了AspectJ的語言
  • Spring AOP僅支持runtime織入,不支持compile time和load time
  • Spring AOP僅支持method執行的join point,不支持field的join point
  • Spring AOP同時支持JDK proxy和CGLIB,代理接口時使用JDK proxy,代理類時使用CGLIB

引用資料

AspectJ:www.eclipse.org/aspectj/doc…

cglib:github.com/cglib/cglib

Spring AOP:docs.spring.io/spring/docs…

Spring Source Code:github.com/spring-proj…

相關文章
相關標籤/搜索