SpringAOP的一些术语意思
SpringAOP总结
- SpringAOP分为JDK实现和CGLib实现。虽然说SpringAOP用到了AspectJ包,但是这只是Spring借用一下AspectJ的注解以实现切入点的解析和匹配,意思是只在注解编程时AspectJ会被用到。AspectJ也是一个用于实现切面编程(AOP)的一个框架,而且有自己的编译器。
Spring官网原文解释:
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使用JDK动态代理实现SpringAOP。
理由如下(摘自Spring官网):
By default, CGLIB is used if a business object does not implement an interface. As it is good practice to program to interfaces rather than classes, business classes normally implement one or more business interfaces.
想要开启CGlib可以在xml中添加一下配置:
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!--默认是false-->
- CGLib的一些特点:
- 使用CGLib代理的类,不需要实现接口,因为CGlib是直接继承那个类实现的代理,这也意味这final关键字会使代理无效甚至报错