我们花了很多篇幅讲解了Spring AOP的实现原理动态代理,但是只有动态代理是不够的,比如说前面提到的Aspect、Join Point、Pointcut等,这些有关AOP的抽象概念也非常重要,我们知道这部分实际上是由AspectJ提供的,那么AspectJ是什么呢?
aspectj is
AspectJ都包括了哪些内容?
或许有人要问,既然AspectJ是需要编译的,那么Spring AOP怎么没有使用额外的编译工具呢?
那是因为Spring仅使用了AspectJ的模型和语言表达式部分。
AspectJ的相关内容非常多,几篇文章都不一定能说的完,这块不是我们讨论的重点,有兴趣的同学请参考附录资料。
在解读源码之前,我们先看下Spring AOP的官方文档,从中我们能获取到极为重要的信息。
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提供的切入点语言。
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三种.
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更像是互补的关系,而不是竞争关系.
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方式.