看到这篇文章,学习一下:http://www.ciaoshen.com/2016/10/28/aop/

想理清一下从“动态代理”,到 “注释”,到“面向切面编程”这么一个技术演进的脉络

只想讲清楚两个问题:

  1. 什么是面向切面编程?为什么要面向切面?
  2. 动态代理技术是怎么实现面向切面的?注释在其中扮演了什么角色?

提到另一篇文章:https://my.oschina.net/huangyong/blog/161338

目前最知名最强大的 Java 开源项目就是 AspectJ 了。Rod Johnson(老罗)写了一个叫做 Spring 框架,从此一炮走红,成为了 Spring 之父。

他在自己的 IOC 的基础之上,又实现了一套 AOP 的框架,后来仿佛发现自己越来越走进深渊里,在不能自拔的时候,有人建议他还是集成 AspectJ 吧,他在万般无奈之下才接受了该建议。于是,我们现在用得最多的想必就是 Spring + AspectJ 这种 AOP 框架了。

Spring AOP:抛出增强

程序报错,抛出异常了,一般的做法是打印到控制台或日志文件中,这样很多地方都得去处理,有没有一个一劳永逸的方法呢?那就是 Throws Advice(抛出增强),它确实很强:

@Component
public class GreetingImpl implements Greeting { @Override
public void sayHello(String name) {
System.out.println("Hello! " + name); throw new RuntimeException("Error"); // 故意抛出一个异常,看看异常信息能否被拦截到
}
}

下面是抛出增强类的代码:

@Component
public class GreetingThrowAdvice implements ThrowsAdvice { public void afterThrowing(Method method, Object[] args, Object target, Exception e) {
System.out.println("---------- Throw Exception ----------");
System.out.println("Target Class: " + target.getClass().getName());
System.out.println("Method Name: " + method.getName());
System.out.println("Exception Message: " + e.getMessage());
System.out.println("-------------------------------------");
}
}

8. Spring AOP:引入增强

这个功能确实太棒了!但还有一个更厉害的增强。如果某个类实现了 A 接口,但没有实现 B 接口,那么该类可以调用 B 接口的方法吗?如果您没有看到下面的内容,一定不敢相信原来这是可行的!

以上提到的都是对方法的增强,那能否对类进行增强呢?用 AOP 的行话来讲,对方法的增强叫做 Weaving(织入),而对类的增强叫做 Introduction(引入)。而 Introduction Advice(引入增强)就是对类的功能增强,它也是 Spring AOP 提供的最后一种增强。

定义了一个新接口 Apology(道歉):

public interface Apology {

    void saySorry(String name);
}

借助 Spring 的引入增强。

@Component
public class GreetingIntroAdvice extends DelegatingIntroductionInterceptor implements Apology { @Override
public Object invoke(MethodInvocation invocation) throws Throwable {
return super.invoke(invocation);
} @Override
public void saySorry(String name) {
System.out.println("Sorry! " + name);
}
}

如何配置

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="aop.demo"/> <bean id="greetingProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interfaces" value="aop.demo.Apology"/> <!-- 需要动态实现的接口 -->
<property name="target" ref="greetingImpl"/> <!-- 目标类 -->
<property name="interceptorNames" value="greetingIntroAdvice"/> <!-- 引入增强 -->
<property name="proxyTargetClass" value="true"/> <!-- 代理目标类(默认为 false,代理接口) -->
</bean> </beans>

客户端代码

public class Client {

    public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("aop/demo/spring.xml");
GreetingImpl greetingImpl = (GreetingImpl) context.getBean("greetingProxy");
      // 注意:转型为目标类,而并非它的 Greeting 接口
greetingImpl.sayHello("Jack"); Apology apology = (Apology) greetingImpl;
      // 将目标类强制向上转型为 Apology 接口(这是引入增强给我们带来的特性,也就是“接口动态实现”功能)
apology.saySorry("Jack");
}
}

以上的示例代码都已经下载:

/Users/baidu/Documents/Data/Work/Code/Demo_Code/aop_demo

下面这篇是续集:

https://my.oschina.net/huangyong/blog/161402

9. Spring AOP:切面

只需要拦截特定的方法就行了,没必要拦截所有的方法。借助了 AOP 的一个很重要的工具,Advisor(切面),来解决这个问题。它也是 AOP 中的核心。

这里提到这个“拦截匹配条件”在 AOP 中就叫做 Pointcut(切点),其实说白了就是一个基于表达式的拦截条件。

归纳一下,Advisor(切面)封装了 Advice(增强)与 Pointcut(切点 )。

下面要做的就是拦截这两个新增的方法,而对 sayHello() 方法不作拦截。

@Component
public class GreetingImpl implements Greeting { @Override
public void sayHello(String name) {
System.out.println("Hello! " + name);
} public void goodMorning(String name) {
System.out.println("Good Morning! " + name);
} public void goodNight(String name) {
System.out.println("Good Night! " + name);
}
}

Spring AOP 中,提供了许多切面类了,这些切面类就有基于正则表达式的切面类。

<?xml version="1.0" encoding="UTF-8"?>
<beans ..."> <context:component-scan base-package="aop.demo"/> <!-- 配置一个切面 -->
<bean id="greetingAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="greetingAroundAdvice"/> <!-- 增强 -->
<property name="pattern" value="aop.demo.GreetingImpl.good.*"/> <!-- 切点(正则表达式) -->
</bean> <!-- 配置一个代理 -->
<bean id="greetingProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="greetingImpl"/> <!-- 目标类 -->
<property name="interceptorNames" value="greetingAdvisor"/> <!-- 切面 -->
<property name="proxyTargetClass" value="true"/> <!-- 代理目标类 -->
</bean> </beans>

注意以上代理对象的配置中的 interceptorNames,它不再是一个增强,而是一个切面,因为已经将增强封装到该切面中了。此外,切面还定义了一个切点(正则表达式),其目的是为了只将满足切点匹配条件的方法进行拦截。

除了 RegexpMethodPointcutAdvisor 以外,在 Spring AOP 中还提供了几个切面类,比如:

DefaultPointcutAdvisor:默认切面(可扩展它来自定义切面)

NameMatchMethodPointcutAdvisor:根据方法名称进行匹配的切面

StaticMethodMatcherPointcutAdvisor:用于匹配静态方法的切面

10. Spring AOP:自动代理(扫描 Bean 名称)

能否让 Spring 框架为我们自动生成代理呢?Spring AOP 提供了一个可根据 Bean 名称来自动生成代理的工具,它就是 BeanNameAutoProxyCreator。是这样配置的:

<?xml version="1.0" encoding="UTF-8"?>
<beans ...> ... <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value="*Impl"/> <!-- 只为后缀是“Impl”的 Bean 生成代理 -->
<property name="interceptorNames" value="greetingAroundAdvice"/> <!-- 增强 -->
<property name="optimize" value="true"/> <!-- 是否对代理生成策略进行优化 -->
</bean> </beans>

关于CGLib和JDK动态代理:

根据多年来实际项目经验得知:CGLib 创建代理的速度比较慢,但创建代理后运行的速度却非常快,而 JDK 动态代理正好相反。
如果在运行的时候不断地用 CGLib 去创建代理,系统的性能会大打折扣,所以建议一般在系统初始化的时候用 CGLib 去创建代理,
并放入 Spring 的 ApplicationContext 中以备后用。

11. Spring AOP:自动代理(扫描切面配置)

Spring AOP 基于切面也提供了一个自动代理生成器:DefaultAdvisorAutoProxyCreator。

<?xml version="1.0" encoding="UTF-8"?>
<beans ...> ... <bean id="greetingAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="pattern" value="aop.demo.GreetingImpl.good.*"/>
<property name="advice" ref="greetingAroundAdvice"/>
</bean> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
<property name="optimize" value="true"/>
</bean> </beans>

这里无需再配置代理了,因为代理将会由 DefaultAdvisorAutoProxyCreator 自动生成。也就是说,这个类可以扫描所有的切面类,并为其自动生成代理。

12. Spring + AspectJ(基于注解:通过 AspectJ execution 表达式拦截方法)

@Aspect
@Component
public class GreetingAspect { @Around("execution(* aop.demo.GreetingImpl.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
before();
Object result = pjp.proceed();
after();
return result;
} private void before() {
System.out.println("Before");
} private void after() {
System.out.println("After");
}
}

方法的参数中包括一个 ProceedingJoinPoint 对象,它在 AOP 中称为 Joinpoint(连接点),可以通过该对象获取方法的任何信息,例如:方法名、参数等。

总结如下图:

再来一张表格,总结一下各类增强类型所对应的解决方案:

增强类型 基于 AOP 接口 基于 @Aspect 基于 <aop:config>
Before Advice(前置增强) MethodBeforeAdvice @Before <aop:before>
AfterAdvice(后置增强) AfterReturningAdvice @After <aop:after>
AroundAdvice(环绕增强) MethodInterceptor @Around <aop:around>
ThrowsAdvice(抛出增强 ThrowsAdvice @AfterThrowing <aop:after-throwing>
IntroductionAdvice(引入增强) DelegatingIntroductionInterceptor @DeclareParents <aop:declare-parents>

最后给一张 UML 类图描述一下 Spring AOP 的整体架构:

以上,需要结合例子深入学习。

(完)

【转载】面向切面编程(AOP)学习的更多相关文章

  1. Spring学习手札(二)面向切面编程AOP

    AOP理解 Aspect Oriented Program面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 但是,这种说法有些片面,因为在软件工程中,AOP的价值体现的并 ...

  2. Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)

    一.面向切面编程AOP 目标:让我们可以“专心做事”,避免繁杂重复的功能编码 原理:将复杂的需求分解出不同方面,将公共功能集中解决 *****所谓面向切面编程,是一种通过预编译方式和运行期动态代理实现 ...

  3. Spring框架学习笔记(2)——面向切面编程AOP

    介绍 概念 面向切面编程AOP与面向对象编程OOP有所不同,AOP不是对OOP的替换,而是对OOP的一种补充,AOP增强了OOP. 假设我们有几个业务代码,都调用了某个方法,按照OOP的思想,我们就会 ...

  4. Spring之控制反转——IoC、面向切面编程——AOP

      控制反转——IoC 提出IoC的目的 为了解决对象之间的耦合度过高的问题,提出了IoC理论,用来实现对象之间的解耦. 什么是IoC IoC是Inversion of Control的缩写,译为控制 ...

  5. Spring框架系列(4) - 深入浅出Spring核心之面向切面编程(AOP)

    在Spring基础 - Spring简单例子引入Spring的核心中向你展示了AOP的基础含义,同时以此发散了一些AOP相关知识点; 本节将在此基础上进一步解读AOP的含义以及AOP的使用方式.@pd ...

  6. 设计模式之面向切面编程AOP

    动态的将代码切入到指定的方法.指定位置上的编程思想就是面向切面的编程. 代码只有两种,一种是逻辑代码.另一种是非逻辑代码.逻辑代码就是实现功能的核心代码,非逻辑代码就是处理琐碎事务的代码,比如说获取连 ...

  7. 【串线篇】面向切面编程AOP

    面向切面编程AOP 描述:将某段代码“动态”的切入到“指定方法”的“指定位置”进行运行的一种编程方式 (其底层就是Java的动态代理)spring对其做了简化书写 场景: 1).AOP加日志保存到数据 ...

  8. 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制

    spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...

  9. [译]如何在ASP.NET Core中实现面向切面编程(AOP)

    原文地址:ASPECT ORIENTED PROGRAMMING USING PROXIES IN ASP.NET CORE 原文作者:ZANID HAYTAM 译文地址:如何在ASP.NET Cor ...

随机推荐

  1. 光学字符识别OCR-5 文本切割

    经过前面文字定位得到单行的文本区域之后,我们就可以想办法将单行的文本切割为单个的字符了.因为第三步的模型是针对单个的字符建立的,因此这一步也是必须的. 均匀切割 基于方块汉字的假设,事实上最简单的切割 ...

  2. cache共享问题

    经测试发现,cache在web中与windows service中是不能共享的.但在windows service可以使用cache.

  3. python面向对象、模块讲解

    (1)模块的介绍: 1.什么是模块  模块是一系列功能的集合体  常见的模块形式(自定义模块.第三方模块.内置模块):   1.一个module.py文件就是一个模块,文件名是module.py,而模 ...

  4. [git 学习篇]版本回退

    再次修改readme.txt ,并将其提交成功 $ git add readme.txt $ git commit -m "append GPL" [master ] append ...

  5. Xshell设置登录会话

    新建会话 点击用户登录验证输入账号密码 如果是公钥登录,选择pubulic key登录

  6. [译]__main__ 顶级脚本环境

    'main'是其中顶级代码执行的范围的名称.一个模块的__name__可以从标准输入,脚本,或从一个交互式命令行中等方式被设置成等于'main'. 一个模块可以发现它是否是通过检查自身在主运行范围__ ...

  7. vs2015代码图

    可以看到代码的调用关系. 知乎文章:IDE 而言,是 Xcode 的技术比较先进还是 Visual Studio?

  8. Unity3D - UGUI的初级应用

    添加字体: 把下载好的字体拖拽到Project面板中 - 点击Text组件中Text属性后面的圆点 - 选择刚刚拖拽的字体即可. 创建ToggleGroup(开关组): 1.在Canvas下创建两个T ...

  9. Bash Command 1: find

    GNU find searches the directory tree rooted at each given starting-point by evaluating the given exp ...

  10. BZOJ1189 [HNOI2007]紧急疏散evacuate 【二分 + 网络流】

    题目 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一 块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一扇门,人们可以从 ...