spring aop与aspectj到底什么关系?

首先,aspectj自己是一个完整的aop框架,没有spring也能跑起来。

其次,Spring AOP是一个专有名词。它专制一种运行机制,不是spring容器中的AOP都叫Spring AOP.

Spring AOP中有很多看上去aspectj的东西,但Spring AOP在运行时,并不会触发aspectj的运行。Spring AOP是基于Proxy机制的,跟aspectj的compiler/weave机制其实没有半毛钱关系 。

虽然代码里这样声明了,但其实还是proxy-based的spring aop.

<aop:aspectj-autoproxy/>

然而,Spring AOP又偏偏基本照搬了aspectj的领域概念和各种语法,并且直接使用了aspectj的相关jar库。 什么@PointCut, @Before等等,都是aspectjrt.jar中直接包括的类。官方文档说,这叫aspectj style.

引用

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.

最后,虽然Spring AOP不是aspectj; 但如果你想直接使用aspectj, 可以利用spring对aspectj的支持。下面这种声明代表真的在用aspectj:

<context:spring-configured/>

结论:

1. Spring AOP的运行没有依托aspectj的运行, 但是在概念和表达式语法层面使用了aspectj的风格

2. 在Spring容器中可以直接使用aspectj,Spring对此一些辅助。这时的AOP已经不是Spring AOP.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.