标准AOP与Spring AOP

AOP介绍

在AOP联盟的官方网站里(http://aopalliance.sourceforge.net/)

AOP是一种能够增强多种已存在的中间件环境(such as J2EE)或者开发环境(e.g. Eclipse)功能的编程技术。

AOP 实现方式与相关项目

关于AOP在很多项目中都有实现,实现的方式也有不同,目前主要有三种处理方式:Proxy(代理), Interceptor(拦截器), bytecode translator(字节码翻译)。

AOP相关的项目有:

  • ASM: a lightweight bytecode translator.
  • AspectJ: an AO source-level weaver. New Language.
  • AspectWerkz: an AO framework (bytecode-level dynamic weaver+configuration).
  • BCEL: a bytecode translator.
  • CGLIB: high-level API for class artifact manipulation and method interception.
  • JAC: an AO middleware (bytecode-level dynamic weaver+configuration+aspects). Framework.
  • Javassist: a bytecode translator with a high-level API.
  • JBoss-AOP: interception and metadata-based AO framework.
  • JMangler: a bytecode translator with a composition framework for translations.
  • Nanning: an AO weaver (framework).
  • Prose: an AO bytecode-level dynamic weaver (framework).
  • ... and many others (email me to add a new one)

而在.Net平台下, PostSharp也是一个有名的AOP框架,它采用的方式与AspectJ类似,AspectJ是通过字节码织入方式,PostSharp是通过修改编译后的文件方式。

AOP分层架构

AOP分层架构中,从概念到实现,分为3个层次:

level 1: 是一种AOP框架的底层处理,是处理Aspect与Target的织入过程,处理方式有:反射、元数据处理、拦截器、代理等等。也就是说,上面提到的几种方式都可以用于

aspect织入到Target上。

level 2: 是AO(面向切面)系统的实现过程。可以在编译期,也可以在运行期。

level 3: 是最高层,这一层中,把面向切面的编程看作是:base + aspect + config。这一层是开发人员直接使用的。

base: 就是target,就是待增加的目标对象。

AOP要用到的技术说明

AOP并不是一门新的技术,而是在已有技术上实现的新功能。

在Level1层:

·反射:没有反射就没有各种框架,现在的AOP框架已有很多,上面已列出常用的。这些框架的底层,都使用到了反射技术。

·拦截器框架:拦截器框架也已有很多,Struts2中有,不是采用动态代理实现的。Java提供了一种标准的拦截器框架,是使用动态代理技术实现的。

·元数据处理:一般来说会和拦截器一起使用,允许扩展类(例如实现新接口)。

·类加载框架:或者通过拦截器框架织入、或者通过字节码织入,都是需要类加载框架的支持。

AOP标准接口

尽管各个AOP框架的实现有所不同,但又一些通用的东西。AOP联盟就把这些通用的东西制定成为AOP标准接口,在aopalliance.1.0.jar里。

这个jar包中的制定的接口很少,但却很重要:

从之前的level 1的组件说明(技术说明)中,了解到:AOP在多处运用到了拦截器,因此AOP联盟也将拦截器封装进这个jar中。

AOP的要义:在JoinPoint加入Advice。

Spring AOP:

AOP 面向切面编程。AOP是Spring的两大核心之一,在Spring中,AOP的典型应用是事务管理。

AOP中有很多概念需要了解:

看官方参考文档中的说明:

  • Aspect: a modularization of a concern that cuts across multiple classes. Transaction management is a good example of a crosscutting concern in enterprise Java applications. In Spring AOP, aspects are implemented using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (the @AspectJ style).

Aspect:切面,是AOP模块的核心,在Spring中,AOP以有规则的类或者注解完成。

 

Join point: a point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution.

连接点,就是sayFoo,sayHello等实际要执行的方法。

  • Advice: action taken by an aspect at a particular join point. Different types of advice include "around," "before" and "after" advice. (Advice types are discussed below.) Many AOP frameworks, including Spring, model an advice as an interceptor, maintaining a chain of interceptors around the join point.

建议就是在sayFoo前后执行的方法,例如之前的befor(),after();

  • Pointcut: a predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.

切点:就是指定在那些地方切入。切点是一个表达式,与这个表达式匹配的方法就是join point。例如表达式为:**.*.say*就是任意packget下任意class的sayXxx方法。方法名以say开头。这个表达式就是pointCut,匹配到的sayHello, sayFoo就是join Point。

  • Introduction: declaring additional methods or fields on behalf of a type. Spring AOP allows you to introduce new interfaces (and a corresponding implementation) to any advised object. For example, you could use an introduction to make a bean implement an IsModified interface, to simplify caching. (An introduction is known as an inter-type declaration in the AspectJ community.)

说明,就可以让一个被advised对象(target)实现某个接口。添加额外功能。

  • Target object: object being advised by one or more aspects. Also referred to as the advised object. Since Spring AOP is implemented using runtime proxies, this object will always be a proxied object.

target,就是要被advised的对象(),也就是说他是被代理的对象。它就相当于上面例子中的FooImpl,HelloImpl对象。

  • AOP proxy: an object created by the AOP framework in order to implement the aspect contracts (advise method executions and so on). In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.

他是根据AOP代理有两种:一种是Java的动态代理,一种是CgLib代理。

  • 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.

织入:这是一个动词,在target(FooImpl)中与point cut (切点表达式, xxx.x.say**)匹配的join point(sayFoo) 的前后加入advice(before(), after(),并且可以另外附加实现Introduction新的接口),最终形成AOP Proxy(Dynamic Proxy, CGLIB Proxy)的过程,称为weaving(织入)。

标准AOP与Spring AOP

时间: 2024-08-05 19:16:12

标准AOP与Spring AOP的相关文章

自己实现 aop 和 spring aop

上文说到,我们可以在 BeanPostProcessor 中对 bean 的初始化前化做手脚,当时也说了,我完全可以生成一个代理类丢回去. 代理类肯定要为用户做一些事情,不可能像学设计模式的时候创建个代理类,然后简单的在前面打印一句话,后面打印一句话,这叫啥事啊,难怪当时听不懂.最好是这个方法的前后过程可以自户自己定义. 小明说,这还不好办,cglib 已经有现成的了,jdk 也可以实现动态代理,看 mybatis 其实也是这么干的,不然你想它一个接口怎么就能找到 xml 的实现呢,可以参照下

Spring源码阅读:使用标准AOP的API模拟Spring AOP + AspectJ的设计与实现

在上一篇博客中,提到了标准AOP与Spring AOP.这一篇就来把他们模拟出来. 在模拟之前,还需要提及的是,在Spring框架中,对于AOP的支持: Spring 支持的AOP AspectJ是另外一个有名的AOP框架,Spring也集成AspectJ,同时Spring AOP与AspectJ有一定程度的集成,这样一来Spring中就支持两种AOP:1)Spring AOP.2)AspectJ.而使用AOP的方式却又三种: 1)完全使用Spring AOP 2)完全使用AspectJ(分为注

Spring AOP学习笔记(1)-概念

1.Aspect 横切在多个类的一个关注点,在Spring AOP中,aspect实现是一个规则的类或@Aspect标注的规则类.例如:事务管理 2.Join point 程序执行过程中的一个点,例如:执行一个方法或处理一个异常,在Spring AOP中,一个连接点表示一个方法执行执行 3.Advice 在一个特定的连接点上所采取的动作,类型包括around,before,after等,Spring中 advice就是一个interceptor模式,包括around连接点的interceptor

[转]彻底征服 Spring AOP 之 理论篇

基本知识 其实, 接触了这么久的 AOP, 我感觉, AOP 给人难以理解的一个关键点是它的概念比较多, 而且坑爹的是, 这些概念经过了中文翻译后, 变得面目全非, 相同的一个术语, 在不同的翻译下, 含义总有着各种莫名其妙的差别. 鉴于此, 我在本章的开头, 着重为为大家介绍一个 Spring AOP 的各项术语的基本含义. 为了术语传达的准确性, 我在接下来的叙述中, 能使用英文术语的地方, 尽量使用英文. 什么是 AOP AOP(Aspect-Oriented Programming),

Spring学习十三----------Spring AOP的基本概念

? 版权声明:本文为博主原创文章,转载请注明出处 什么是AOP -面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 -主要的功能是:日志记录.性能统计.安全控制.事务处理.异常处理等 AOP实现方式 -预编译 -AspectJ -运行期动态代理(JDK动态代理.CGLib动态代理) -Spring AOP.Jboss AOP AOP相关概念 -切面(Aspect)   一个关注点的模块化,这个关注点可能会横切多个对象 -连接点(Joinpoint)   程序执行过程中

Spring AOP的本质

不用再百科什么AOP了,我先推荐几篇文章或者系列文章:(感谢这些博文的原作者) 0.  Spring AOP 详解   http://pandonix.iteye.com/blog/336873/ 1.  AOP技术基础系列     http://wayfarer.cnblogs.com/articles/241024.html 2.  我对AOP的理解 http://jinnianshilongnian.iteye.com/blog/1474325 3.  Spring AOP本质系列  ht

浅析Spring AOP(面向方面编程)

SpringAOP提供的优势 1.允许开发者声明企业级服务,比如:事务服务.安全性服务.EJB组件能够使用J2EE容器提供声明式服务.但是需要借助于EJB组件,而SpringAOP却不需要EJB容器,即借助于Spring的事务抽象框架能够在EJB容器外部使用企业级.声明式服务. 2.开发者可以开发满足业务需求的自定义方面.类似于JBOSS服务器中拦截器开发一样,如果标准的J2EE安全性不能满足业务需求,则必须开发拦截器. 3.开发SpringAOP advice很方便,这些AOP Advice不

彻底征服 Spring AOP 之 理论篇

基本知识 其实, 接触了这么久的 AOP, 我感觉, AOP 给人难以理解的一个关键点是它的概念比较多, 而且坑爹的是, 这些概念经过了中文翻译后, 变得面目全非, 相同的一个术语, 在不同的翻译下, 含义总有着各种莫名其妙的差别. 鉴于此, 我在本章的开头, 着重为为大家介绍一个 Spring AOP 的各项术语的基本含义. 为了术语传达的准确性, 我在接下来的叙述中, 能使用英文术语的地方, 尽量使用英文. 什么是 AOP AOP(Aspect-Oriented Programming),

Spring AOP 关键词的理解

1.如下图所示: AOP的执行就是在什么时候,什么地方,做什么. 2.关键词理解: 连接点(JoinPoint): 就是能够作为切点的一个个动作(方法),当然实际上不是所有连接点都当做切点的. 切点(Poincut):链接点中的一个或多个,切面会在这些点上来做文章(切点就是什么地方). 通知(Advice):通知是在切点上什么时候,做什么. 通知有下列几种类型:Before,After,After-returning, After-throwing,Around 切面(Aspect):切面包括切