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…

相关文章
相关标签/搜索