一、什么是 AOP。

AOP(Aspect Orient Programming),也就是面向切面编程。能够这样理解,面向对象编程(OOP)是从静态角度考虑程序结构,面向切面编程(AOP)是从动态角度考虑程序执行过程

二、AOP 的作用。

经常通过 AOP 来处理一些具有横切性质的系统性服务,如事物管理、安全检查、缓存、对象池管理等,AOP 已经成为一种很经常使用的解决方式。

三、AOP 的实现原理。

如图:AOP 实际上是由目标类的代理类实现的AOP 代理事实上是由 AOP 框架动态生成的一个对象,该对象可作为目标对象使用。AOP 代理包括了目标对象的所有方法,但 AOP 代理中的方法与目标对象的方法存在差异,AOP
方法在特定切入点加入了增强处理,并回调了目标对象的方法

四、Spring 中对 AOP 的支持

Spring 中 AOP 代理由 Spring 的 IoC 容器负责生成、管理,其依赖关系也由 IoC 容器负责管理。因此,AOP 代理能够直接使用容器中的其它 Bean 实例作为目标,这样的关系可由 IoC 容器的依赖注入提供。Spring 默认使用 Java
动态代理来创建 AOP 代理
, 这样就能够为不论什么接口实例创建代理了。当须要代理的类不是代理接口的时候, Spring 自己主动会切换为使用 CGLIB 代理,也可强制使用 CGLIB

AOP 编程事实上是非常easy的事情。纵观 AOP 编程, 当中须要程序猿參与的仅仅有三个部分:

  • 定义普通业务组件。
  • 定义切入点,一个切入点可能横切多个业务组件。
  • 定义增强处理,增强处理就是在 AOP 框架为普通业务组件织入的处理动作。

所以进行 AOP 编程的关键就是定义切入点和定义增强处理。一旦定义了合适的切入点和增强处理,AOP 框架将会自己主动生成 AOP 代理,即:代理对象的方法 = 增强处理 + 被代理对象的方法

五、Spring 中 AOP 的实现。

Spring 有例如以下两种选择来定义切入点和增强处理。

  • 基于 Annotation 的“零配置”方式:使用@Aspect、@Pointcut等 Annotation 来标注切入点和增强处理。
  • 基于 XML 配置文件的管理方式:使用 Spring 配置文件来定义切入点和增强点。

1、基于 Annotation 的“零配置”方式。

(1)、首先启用 Spring 对 @AspectJ 切面配置的支持。

<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/beans/spring-aop-3.0.xsd">
<!-- 启动对@AspectJ注解的支持 -->
<aop:aspectj-autoproxy/>
</beans>

假设不打算使用 Spring 的 XML Schema 配置方式,则应该在 Spring 配置文件里添加例如以下片段来启用@AspectJ 支持。

<!-- 启用@AspectJ 支持 -->
<bean class="org.springframeword.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />

(2)、定义切面 Bean。

当启动了@AspectJ 支持后,仅仅要在 Spring 容器中配置一个带@Aspect 凝视的 Bean, Spring 将会自己主动识别该 Bean 并作为切面处理。

// 使用@Aspect 定义一个切面类
@Aspect
public class LogAspect {
// 定义该类的其它内容
...
}

(3)、定义 Before 增强处理。

// 定义一个切面
@Aspect
public class BeforeAdviceTest {
// 匹配 com.wicresoft.app.service.impl 包下全部类的全部方法作为切入点
@Before("execution(* com.wicresoft.app.service.impl.*.*(..))")
public void authorith(){
System.out.println("模拟进行权限检查。");
}
}

上面使用@Before Annotation 时,直接指定了切入点表达式,指定匹配 com.wicresoft.app.service.impl包下全部类的全部方法运行作为切入点。

关于这个表达式的规则例如以下图。

(4)、定义 AfterReturning 增强处理。

// 定义一个切面
@Aspect
public class AfterReturningAdviceTest {
// 匹配 com.wicresoft.app.service.impl 包下全部类的全部方法作为切入点
@AfterReturning(returning="rvt", pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))")
public void log(Object rvt) {
System.out.println("模拟目标方法返回值:" + rvt);
System.out.println("模拟记录日志功能...");
}
}

(5)、定义 AfterThrowing 增强处理。

// 定义一个切面
@Aspect
public class AfterThrowingAdviceTest {
// 匹配 com.wicresoft.app.service.impl 包下全部类的全部方法作为切入点
@AfterThrowing(throwing="ex", pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))")
public void doRecoverActions(Throwable ex) {
System.out.println("目标方法中抛出的异常:" + ex);
System.out.println("模拟抛出异常后的增强处理...");
}
}

(6)、定义 After 增强处理。

After 增强处理与AfterReturning 增强处理有点相似,但也有差别:

  • AfterReturning 增强处理处理仅仅有在目标方法成功完毕后才会被织入。

  • After 增强处理无论目标方法怎样结束(保存成功完毕和遇到异常中止两种情况),它都会被织入。
// 定义一个切面
@Aspect
public class AfterAdviceTest {
// 匹配 com.wicresoft.app.service.impl 包下全部类的全部方法作为切入点
@After("execution(* com.wicresoft.app.service.impl.*.*(..))")
public void release() {
System.out.println("模拟方法结束后的释放资源...");
}
}

(7)、Around 增强处理

Around 增强处理近似等于 Before 增强处理和  AfterReturning 增强处理的总和。它可改变运行目标方法的參数值,也可改变目标方法之后的返回值。

// 定义一个切面
@Aspect
public class AroundAdviceTest {
// 匹配 com.wicresoft.app.service.impl 包下全部类的全部方法作为切入点
@Around("execution(* com.wicresoft.app.service.impl.*.*(..))")
public Object processTx(ProceedingJoinPoint jp) throws java.lang.Throwable {
System.out.println("运行目标方法之前,模拟開始事物...");
// 运行目标方法,并保存目标方法运行后的返回值
Object rvt = jp.proceed(new String[]{"被改变的參数"});
System.out.println("运行目标方法之前,模拟结束事物...");
return rvt + "新增的内容";
}
}

(8)、訪问目标方法的參数。

訪问目标方法最简单的做法是定义增强处理方法时将第一个參数定义为 JoinPoint 类型,当该增强处理方法被调用时,该 JoinPoint 參数就代表了织入增强处理的连接点。JoinPoint 里包括了例如以下几个经常用法。

  • Object[] getArgs(): 返回运行目标方法时的參数。
  • Signature getSignature(): 返回被增强的方法的相关信息。
  • Object getTarget(): 返回被织入增强处理的目标对象。
  • Object getThis(): 返回 AOP 框架为目标对象生成的代理对象。

提示:当时使用 Around 处理时,我们须要将第一个參数定义为 ProceedingJoinPoint 类型,该类型是 JoinPoint 类型的子类

(9)、定义切入点。

所谓切入点,事实上质就是为一个切入点表达式起一个名称,从而同意在多个增强处理中重用该名称。

Spring 切入点定义包括两个部分:

  • 一个切入点表达式。
  • 一个包括名字和随意參数的方法签名。
// 使用@Pointcut Annotation 时指定切入点表达式
@pointcut("execution * transfer(..)")
// 使用一个返回值为void,方法体为空的方法来命名切入点
private void anyOldTransfer(){} // 使用上面定义的切入点
@AfterReturning(pointcut="anyOldTransfer()", returning="reVal")
public void writeLog(String msg, Object reVal){
...
}

2、基于 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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/beans/spring-aop-3.0.xsd">
<aop:config>
<!-- 将 fourAdviceBean 转换成切面 Bean, 切面 Bean 的新名称为:fourAdviceAspect,指定该切面的优先级为2 -->
<aop:aspect id="fourAdviceAspect" ref="fourAdviceBean" order="2">
<!-- 定义个After增强处理,直接指定切入点表达式,以切面 Bean 中的 Release() 方法作为增强处理方法 -->
<aop:after pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="release" /> <!-- 定义个Before增强处理,直接指定切入点表达式,以切面 Bean 中的 authority() 方法作为增强处理方法 -->
<aop:before pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="authority" /> <!-- 定义个AfterReturning增强处理,直接指定切入点表达式,以切面 Bean 中的 log() 方法作为增强处理方法 -->
<aop:after-returning pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="log" /> <!-- 定义个Around增强处理,直接指定切入点表达式,以切面 Bean 中的 processTx() 方法作为增强处理方法 -->
<aop:around pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="processTx" /> </aop:aspect>
</aop:config> <!-- 省略各个Bean 的配置 -->
<!-- ... --> </beans>
  • 配置切入点
<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/beans/spring-aop-3.0.xsd">
<aop:config>
<!-- 定义一个切入点,myPointcut,直接知道它相应的切入点表达式 -->
<aop:pointcut id="myPointcut" expression="execution(* com.wicresoft.app.service.impl.*.*(..))" method="release" />
<aop:aspect id="afterThrowingAdviceAspect" ref="afterThrowingAdviceBean" order="1">
<!-- 使用上面定于切入点定义增强处理 -->
<!-- 定义一个AfterThrowing 增强处理,指定切入点以切面 Bean 中的 doRecovertyActions() 方法作为增强处理方法 -->
<aop:after-throwing pointcut-ref="myPointcut" method="doRecovertyActions" throwing="ex" />
</aop:aspect>
</aop:config> <!-- 省略各个Bean 的配置 -->
<!-- ... --> </beans>

參考:

《轻量级 Java EE 企业应用实战(第三版)》 李刚

Spring3.0 AOP 具体解释的更多相关文章

  1. Spring3.0 AOP 详解

    一.什么是 AOP. AOP(Aspect Orient Programming),也就是面向切面编程.可以这样理解,面向对象编程(OOP)是从静态角度考虑程序结构,面向切面编程(AOP)是从动态角度 ...

  2. spring3.0.5的aop使用

    spring3.0.5开始支持jpa2.0了,但是最近笔者在使用他的的时候发现了3.0.5的包与2.5.5相比,有所精简.其他外部的包,我们需要自己下载. AOP必须的spring包 org.spri ...

  3. spring3.0使用annotation完全代替XML

    @Service与@Component有什么不同?那天被问到这个问题,一时之间却想不起来,就利用这篇文章来纪录spring3.0中常用的annotation. 从spring2.5开始,annotat ...

  4. Spring3.0 与 MyBatis框架 整合小实例

    本文将在Eclipse开发环境下,采用Spring MVC + Spring + MyBatis + Maven + Log4J 框架搭建一个Java web 项目. 1. 环境准备: 1.1 创建数 ...

  5. 开发基础框架:mybatis-3.2.8 +hibernate4.0+spring3.0+struts2.3

    一:项目下载地址(点击 Source code(zip)) https://github.com/fzxblgong/frame_2014-12-15/releases 版本:v1.2大小:20M 二 ...

  6. spring3.0的jar包详解

    1. spring.jar 是包含有完整发布模块的单个jar 包. 2. org.springframework.aop 包含在应用中使用Spring的AOP特性时所需的类. 3. org.sprin ...

  7. 《Spring3.0就这么简单》

    第一章 认识Spring 1.Spring提供的IOC容器,是Spring大杀器之一.容器将对象之间的依赖关系交给Spring进行控制,采用配制的方式对依赖关系进行描述,由Ioc容器负责依赖类之间的创 ...

  8. Maven整合Spring3.0+Mybatis3.2+Struts2.3+查找坐标+jar包依赖(五)

    依赖传递 只添加了一个struts2-core依赖,发现项目中出现了很多jar,这种情况 叫 依赖传递

  9. MyEclipse-10.0下Struts2.1+Spring3.0+Hibernate3.3整合过程

    新建web project: 命名为SSH,做如下设置: 新建后的工程目录如下: 然后开始添加SSH框架,这里我按照struts-spring-hibernate顺序进行添加. 首先添加struts2 ...

随机推荐

  1. var a=[]; 和 var a=new Array(); 的区别,为什么前者效率高

    因为 JSON格式的语法是引擎直接解释的.而new Array 则需要调用Array的构造器.还有就是1.当你需要将一个数字转化为字符串时可以这样定义:var s=""+1; 这样 ...

  2. java数组或集合返回空的问题

    在有返回值的情况下,平时我写代码基本遇到错误什么都是返回null,我因为我觉得把数组或集合这个初始化占空间. 但是我发现这样在每次客户段调用都要进行非空判断,而且有时调用内置api还容易报错误,于是解 ...

  3. html5学习链接

    http://www.runoob.com/tags/html-colorpicker.html

  4. 【POJ】1505 Copying Books

    此题主要采用DP思路,难点是求解"/",需要考虑划分数量不够的情况,先采用DP求解最优解,然后采用贪心求解slash.防止中间结果出错,使用了unsigned int. #incl ...

  5. URAL1009

    链接 第一道URAL题 简单递推 #include <iostream> #include<cstdio> #include<cstring> #include&l ...

  6. LA_4670_Dominating_Patterns_(AC自动机+map)

    描述 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  7. Azure Backup 入门

    Viswanath Tata 云 + Enterprise项目经理 Azure Backup是一款允许客户将数据备份到 Azure的强大工具.请参阅这篇文章,快速了解 Azure Backup.我 ...

  8. ORACLE RAC NTP 时间服务器配置

    Linux 时间同步配置 . 一. 使用ntpdate 命令 1.1 服务器可链接外网时 # crontab -e 加入一行: */1 * * * * ntpdate 210.72.145.44 21 ...

  9. CSS3/SVG clip-path路径剪裁遮罩属性简介

    一.SVG属性和CSS3属性千丝万缕的关系 CSS3新增属性除了我们现在用的比较多的border-radius, box-shadow, gradient, ...之类,还有很重要的一个分支:SVG属 ...

  10. 一例胜千言,详谈SQL Sever数据库锁

    1 前言 数据库大并发操作要考虑死锁和锁的性能问题.看到网上大多语焉不详(尤其更新锁),所以这里做个简明解释,为下面描述方便,这里用T1代表一个数据库执行请求,T2代表另一个请求,也可以理解为T1为一 ...