学技术学英文:Spring AOP和 AspectJ 的关系
AspectJ是AOP领域的江湖一哥, Spring AOP 只是一个小弟
Spring AOP is implemented in pure Java. There is no need for a special compilation process. Spring AOP does not need to control the class loader hierarchy and is thus suitable for use in a servlet container or application server.
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’s approach to AOP differs from that of most other AOP frameworks. The aim is not to provide the most complete AOP implementation (although Spring AOP is quite capable). Rather, the aim is to provide a close integration between AOP implementation and Spring IoC, to help solve common problems in enterprise applications.
Thus, for example, the Spring Framework’s AOP functionality is normally used in conjunction with the Spring IoC container. Aspects are configured by using normal bean definition syntax (although this allows powerful "auto-proxying" capabilities). This is a crucial difference from other AOP implementations. You cannot do some things easily or efficiently with Spring AOP, such as advise very fine-grained objects (typically, domain objects). AspectJ is the best choice in such cases. However, our experience is that Spring AOP provides an excellent solution to most problems in enterprise Java applications that are amenable to AOP.
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 seamlessly integrates Spring AOP and IoC with AspectJ, to enable all uses of AOP within a consistent Spring-based application architecture. This integration does not affect the Spring AOP API or the AOP Alliance API. Spring AOP remains backward-compatible. See the following chapter for a discussion of the Spring AOP APIs.
Spring AOP 和 AspectJ 的关系:
因为AspectJ比较强大,所以spring能够解释与 AspectJ 5 相同的注解,并且它使用 AspectJ 提供的一个库来解析和匹配切入点(pointcut)
用了aspectJ的设计理念、接口、甚至代码,但是没有直接引用aspectJ的jar包,这个过程是怎么实现的自己体会。
@AspectJ refers to a style of declaring aspects as regular Java classes annotated with annotations. The @AspectJ style was introduced by the AspectJ project as part of the AspectJ 5 release. Spring interprets the same annotations as AspectJ 5, using a library supplied by AspectJ for pointcut parsing and matching. The AOP runtime is still pure Spring AOP, though, and there is no dependency on the AspectJ compiler or weaver.
手动开启AspectJ @EnableAspectJAutoProxy
当你需要AOP的高级功能且spring aop 不能满足时,需要在spring中手动开启AspectJ,并且需要导入aspect的jar包
To use @AspectJ aspects in a Spring configuration, you need to enable Spring support for configuring Spring AOP based on @AspectJ aspects and auto-proxying beans based on whether or not they are advised by those aspects. By auto-proxying, we mean that, if Spring determines that a bean is advised by one or more aspects, it automatically generates a proxy for that bean to intercept method invocations and ensures that advice is run as needed.
The @AspectJ support can be enabled with programmatic or XML configuration. In either case, you also need to ensure that AspectJ’s org.aspectj:aspectjweaver
library is on the classpath of your application (version 1.9 or later).
总结
因为Aspectj 非常强大,所以spring aop借用甚至是复用了 aspectj的注解、注解解析相关部分的代码,spring aop 代理机制是自己实现的,spring aop没有依赖aspectj的jar包,所以代码层面spring aop 和 aspectj 实际上没直接关系的。在spring 里面你也可以导入aspectj ,并开启@EnableAspectJAutoProxy ,直接使用aspectj 。spring aop 和 aspectj 都是做AOP的,但代码层面是完全2个东西 , 在spring里面你也可以很方便集成使用aspectj ,就像spring里使用mybatis等其他组件一样。