Spring4.3x教程之三AOP详解

来源:互联网 发布:python财经数据接口包 编辑:程序博客网 时间:2024/05/22 22:16

这篇文章,咱们聊聊Spring中的AOP编程的实现,下面是摘自官网的一些话:
Aspect-Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects. (Such concerns are often termed crosscutting concerns in AOP literature.)
大意:面向方面的编程(AOP)通过提供另一种方法来补充面向对象的编程(OOP)。在OOP中,模块化的关键单元是类,而在AOP中,模块化的单位是方面。方面使事务管理等关注点的模块化得以实现,例如跨越多个类型和对象的事务管理。(这种担忧经常被称为AOP文学中的横切关注点。)
AOP is used in the Spring Framework to:
1、provide declarative enterprise services, especially as a replacement for EJB declarative services. The most important such service is declarative transaction management.
2、allow users to implement custom aspects, complementing their use of OOP with AOP.
AOP 是在 Spring 框架使用的…
1、提供声明式企业服务,特别是作为EJB声明服务的替代。最重要的服务是声明式事务管理。
2、允许用户实现自定义方面,并与AOP一起使用OOP。

OOP:Object-Oriented Programming 面向对象编程
就是万事万物皆对象
类和对象
AOP:Aspect-Oriented Programming面向切面编程
我们以横切面来进行开发
切入点和切面
比如我们使用OOP的时候,多个类之间的关系一般都是纵向关系。那么如果我们只关心具体的某些方法呢(这些方法在不同的类中)?OOP还能很好的搞定吗?再比如一个类中有10个属性,但是我们只需要其中的一个属性,通过OOP那么这10个属性你都需要拥有,会不会很浪费呢?
总而言之OOP的缺陷:
1、纵向关系
2、最小分割的是类
而AOP就是对OOP的弥补:
1、横向关系
2、以面为最小单位,至于说面的大小完全取决于切入点的写法
归根结底:AOP可以让我们在不改变源码的情况下对某些方法的功能做出改变。
AOP的切入点可以是属性、方法、构造器,但是Spring引用时只是保留了方法。所以Spring AOP就是基于方法的切面编程的实现。
那么我们在使用AOP的时候,需要明白几个核心概念:
1、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).
1、切面:对跨多个类进行切割的关注的模块化。事务管理是企业Java应用程序横切关注的一个很好的例子。在Spring AOP中,方面是使用常规类(基于模式的方法)实现的,或者使用@ aspect注释(@ aspectj样式)注释的规则类。
2、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.
2、连接点:在执行程序时的一个点,例如执行方法或处理异常。在Spring AOP中,连接点总是表示方法执行。
3、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.
3、通知:在特定连接点上的一个方面所采取的行动。不同类型的建议包括“around”,“before”和“after”建议。(以下是建议类型。)许多AOP框架,包括Spring,将一个建议作为拦截器,在连接点周围维护一个拦截器链。
4、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.
4、切入点:匹配连接点的谓词。建议与切入点表达式相关联,并在切入点(例如,具有特定名称的方法的执行)匹配的任何连接点上运行。连接点与切入点表达式匹配的概念是AOP的核心,Spring在默认情况下使用AspectJ切点表达式语言。
5、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.)
5、引入:代表类型声明其他方法或字段。Spring AOP允许您向任何被建议的对象引入新的接口(以及相应的实现)。例如,您可以使用介绍使bean实现一个IsModified接口,以简化缓存。(在AspectJ社区中,介绍称为跨类型声明。)
6、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.
6、目标类:也叫委托类:被一个或多个方面建议的对象。也称为被建议的对象。由于Spring AOP是使用运行时代理实现的,所以这个对象将永远是一个代理对象。
7、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.
7、代理类:一个由AOP框架创建的对象,以实现方面契约(通知方法执行等)。在Spring框架中,AOP代理将是一个JDK动态代理或CGLIB代理。
8、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.
8、织入:将各方面与其他应用程序类型或对象链接起来,创建一个建议对象。这可以在编译时完成(例如,使用AspectJ编译器),加载时间,或者运行时。与其他纯Java AOP框架一样,Spring AOP在运行时执行编织
上述就是AOP的八大核心概念:
简单来讲:
我们需要创建通知类也就是我们需要对方法的额外实现的功能
然后我们再定义切入点,目的是获取跟切入点匹配的切面。有了切面我们就可以在连接点处设置什么时候执行我们通知中的方法。那如何执行通知中的方法呢,借助于代理模式实现的代理类。
使用Spring AOP的步骤:
1、创建通知类

public class SQLTime {    long start;    //之前记录    public void startTime() {        start=System.currentTimeMillis();    }    //之后结果    public void endTime() {        System.out.println("本次时间:"+(System.currentTimeMillis()-start));    }}

2、配置AOP
在applicationContext.xml中实现配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd">    <!-- 配置dao对象 -->    <bean id="dao1" class="cn.code404.dao.UserDao" scope="prototype"/>    <!--配置service对象  -->    <bean id="service1" class="cn.code404.service.UserService" scope="prototype">    <property name="dao" ref="dao1"></property>    </bean>    <!--统计dao包下的方法的执行次数  -->    <!--1、配置通知类对象  -->    <bean id="advice1" class="cn.code404.advice.SQLTime"></bean>    <!-- aop切面的配置信息 -->    <aop:config>    <!--定义切入点    切入点的表达式的意思:    1、* 返回值类型    2、包名 *    3、类名 *    4、方法名 *  -->    <aop:pointcut expression="execution(* cn.code404.dao.*Dao.*(..))" id="p1"/>    <!--创建横切面  -->    <aop:aspect ref="advice1" id="ap1">    <!--配置通知执行的时机    前:before    后:after    环绕:前和后    成功返回:after-returning    返回异常:after-throwing  -->    <aop:before method="startTime" pointcut-ref="p1"/>    <aop:after method="endTime" pointcut-ref="p1"/>    </aop:aspect>    </aop:config></beans>

上述就是使用AOP实现对查询方法的只需时间的统计。如果不是呀AOP那么需要在dao层的select语句之前和之后修改记录时间。可是使用了AOP我们就可以不用去改变Dao层的代码只是在执行方法时自动的将通知中的方法给执行就可以了。
其实用好切面就记住三个单词就够了:
what:干什么
使用AOP要达到什么目的,也就是我们的通知
where:在哪里
对哪些方法加通知:切入点
when:什么时候
在什么时候去执行通知的方法
也就是配置切面的通知出现的时机,前、后、异常、环绕等。