On the Spring (two) AOP Atlas

Spring AOP second generation AOP. AOP uses a Java implementation of the implementation language (AOL), and the dynamic proxy mechanism byte code generation techniques.

Proxy design pattern

  • ISubject of interviewees or abstract resources being accessed, without the use of some scenarios similar unified abstract interface can also be
  • SubjectImpl implementation class
  • SubjectProxy agency implementation class
  • Client representative of abstract roles visitors

Acting is not limited to forwarding the request, more often request to add more access restrictions or simplify the use of
the Spring AOP proxy essence is to use this mechanism to achieve

AOP Implementation Mechanism on the Java platform

  • Dynamic proxy
    dynamic proxy mechanism JDK 1.3 is introduced as the corresponding interfaces dynamically generated corresponding to the proxy object, SPring AOP uses this mechanism by default. However, the use of dynamic proxy can only achieve a corresponding Interface class.
  • Dynamic bytecode enhancement
    using ASM or CGLIB and other Java tools library during program execution, dynamically build class file byte code, as long as the Java virtual machine to load the class specification on the line. No need to interface the only restriction is that can not be overwritten for final approach.
  • Java code generation
    relatively old, retired
  • Custom class loader
    all clas Java programs can be run after the corresponding class loader loaded into the Java virtual machine. Powerful ways than before, but increase the burden of class loader itself
  • AOL expansion
    of the most powerful, the most difficult to master.

Spring AOP

The basic concept to achieve AOP

  • Joinpoint
    only supports method-level Joinpoint (Metho Execution), to meet the development needs of 80%.
  • Pointcut

  • Advice

  • Aspect
    Advisor代表Spring中的Aspect,但是,与正常的Aspect不两只,Advisor通常只持有一个Pointcut和一个Advice。而理论上,Aspect定义可以有多个Pointcut和Advice

织入 ProxyFactory

俗话说得好,“万事俱务,只欠东风”,各个模块已经实现了,最后一步就是拼装了。
要进行织入,AspectJ采用ajc编译器;JBoss AOP使用自定义的ClassLoader,而Spring AOP使用类org.springframework.aop.framework.ProxyFactory作为织入器。

几种Spring AOP

  • Spring AOP 1.x 基于接口定义的的Advice声明方式:各种类型的Adivce定义需要实现特定的接口,Advice的管理可以通过IoC容器或者直接编程进行。
  • @AspectJ 2.0之后新增加,只需要以POJO形式声明相应的Aspect和Advice,然后通过相应的注解标注一下即可。各种信息的管理统一到一个位置,并且由于IDE的重构支持,AOP的管理更加方便高效。
  • 基于Schema的AOP。融合第一代和第二代。各种信息转移到XSD形式的容器配置文件中。
    应该依照当前应用环境、各种工具支持、整体团队能力等不同情况来权衡利弊。

Guess you like

Origin www.cnblogs.com/yeni/p/12073634.html