一、先看一个计算器的抽取和实现

  • 图中,有两个需求

    • 需求1-日志:在程序执行期间追踪正在发生的活动
    • 需求2-验证:希望计算器只能处理正数的运算
  • 其中实现的代码片段

从代码片段中可以看出,代码混乱,代码分散,如果日志需求变更,必须修改所有方法。

二、使用动态代理解决以上问题。

1 设计原理

  • 代理设计模式的原理: 使用一个代理将对象包装起来, 然后用该代理对象取代原始对象. 任何对原始对象的调用都要通过代理. 代理对象决定是否以及何时将方法调用转到原始对象上.

2 代码实现

2.1 接口代码

public interface ArithmeticCalculator {
int add(int i, int j);
int sub(int i, int j); int mul(int i, int j);
int div(int i, int j);
}

2.2 实现接口的代码

public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
int result = i + j;
return result;
}
@Override
public int sub(int i, int j) {
int result = i - j;
return result;
}
@Override
public int mul(int i, int j) {
int result = i * j;
return result;
}
@Override
public int div(int i, int j) {
int result = i / j;
return result;
}
}

2.3 测试代码

    public static void main(String[] args) {
ArithmeticCalculator arithmeticCalculator = new ArithmeticCalculatorImpl(); int result = arithmeticCalculator.add(11, 12);
System.out.println("result:" + result); result = arithmeticCalculator.div(21, 3);
System.out.println("result:" + result);
}

打印出

result:23

result:7

这个是原始的代码,接下来,我们加入动态代理

2.3 创建动态代理类

public class ArithmeticCalculatorLoggingProxy {
// 要代理的对象
private ArithmeticCalculator target;
public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target) {
super();
this.target = target;
}
// 返回代理对象
public ArithmeticCalculator getLoggingProxy() {
ArithmeticCalculator proxy = null;
ClassLoader loader = target.getClass().getClassLoader();
Class[] interfaces = new Class[] { ArithmeticCalculator.class };
InvocationHandler h = new InvocationHandler() {
/**
* proxy: 代理对象。 一般不使用该对象 method: 正在被调用的方法 args: 调用方法传入的参数
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
// 打印日志
System.out.println("[before] The method " + methodName + " begins with " + Arrays.asList(args));
// 调用目标方法
Object result = null;
try {
// 前置通知
result = method.invoke(target, args);
// 返回通知, 可以访问到方法的返回值
} catch (NullPointerException e) {
e.printStackTrace();
// 异常通知, 可以访问到方法出现的异常
}
// 后置通知. 因为方法可以能会出异常, 所以访问不到方法的返回值
// 打印日志
System.out.println("[after] The method ends with " + result);
return result;
}
};
/**
* loader: 代理对象使用的类加载器。
* interfaces: 指定代理对象的类型. 即代理代理对象中可以有哪些方法.
* h: 当具体调用代理对象的方法时, 应该如何进行响应, 实际上就是调用 InvocationHandler 的 invoke 方法
*/
proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h);
return proxy;
}
}

2.3.1 测试动态代理

    public static void main(String[] args) {
ArithmeticCalculator arithmeticCalculator = new ArithmeticCalculatorImpl(); arithmeticCalculator =
new ArithmeticCalculatorLoggingProxy(arithmeticCalculator).getLoggingProxy(); int result = arithmeticCalculator.add(11, 12);
System.out.println("result:" + result); result = arithmeticCalculator.div(21, 3);
System.out.println("result:" + result);
}

打印出:

[before] The method add begins with [11, 12]

[after] The method ends with 23

result:23

[before] The method div begins with [21, 3]

[after] The method ends with 7

result:7

2.4 动态代理类代码说明

代理类中, Proxy.newProxyInstance(loader, interfaces, h); 这个类需要传递三个参数

  • loader: 代理对象使用的类加载器。使用目标方法的类加载器即可
  • interfaces: 指定代理对象的类型. 即代理代理对象中可以有哪些方法.
  • h: 当具体调用代理对象的方法时, 应该如何进行响应, 实际上就是调用 InvocationHandler 的 invoke 方法

传递的h中InvocationHandler 的参数介绍:

  • proxy: 代理对象。 一般不使用该对象
  • method: 正在被调用的方法,调用method.invoke(目标对象, args);
  • args: 调用方法传入的参数

    public Object invoke(Object proxy, Method method, Object[] args)

2.5 前置通知,后置通知,返回通知,异常通知

在invoke方法中,可以看出,在方法前后分别可以打印日志,发生异常时,也可以在catch中对异常的处理,这几个不同的位置,分别为 : 前置通知,后置通知,返回通知,异常通知

前置通知 : 调用目标方法之前执行的代码

后置通知 : 调用目标方法之后执行的代码

返回通知 : 可以得到目标方法返回值,从而进一步处理,在method.invoke(目标对象, args);之后

异常通知 : 调用目标方法出异常时,catch中执行的代码,此时,返回通知无法别执行

整个系列项目代码: http://git.oschina.net/nmc5/spring

最新文章

  1. Python【第二章】:Python的数据类型
  2. 413 Request Entity Too Large
  3. IOS Animation-CAKeyframeAnimation例子(简单动画实现)
  4. mybatis学习之路
  5. Spring源码学习(二)AOP
  6. 九度OJ 1205 N阶楼梯上楼问题 -- 动态规划(递推求解)
  7. 【转】iOS开发入门:Xcode常用快捷键
  8. ajax防止重复提交请求1
  9. 简单聊聊TestNG中的并发
  10. protobuf是什么?
  11. [IOT] 自制蓝牙工牌办公室定位系统 (一)—— 阿里物联网平台概览及打通端到云(硬核·干货)
  12. Django 配置(一)开启服务
  13. SSM框架整合过程总结
  14. mysql报错问题解决MySQL server PID file could not be found!
  15. LeetCode 151 翻转字符串里的单词
  16. UML中的6大关系(关联、依赖、聚合、组合、泛化、实现)
  17. 根据前面的FtpUtil写一个demo
  18. 20145221 《Java程序设计》实验报告五:网络编程及安全
  19. Java reflect 反射 2
  20. [User Defaults] Failed to read values in CFPrefsPlistSource (iOS 10)

热门文章

  1. oracle聚合函数avg()注意点
  2. ssm项目配置多个数据源
  3. What size do you use for varchar(MAX) in your parameter declaration?
  4. 个人笔记 - C++相关收藏
  5. seleniumIDE command命令
  6. Tomcat负载均衡、调优核心应用进阶学习笔记(二):Tomcat前世今生、安装、配置文件详细说明、tomcat应用程序部署、webapp 体系结构、tomcat运行方式
  7. Oracle之Group by和Having-----转了
  8. 32. 持续集成简介及JDK、Tomcat、Jenkins环境搭建
  9. selenium 3 下载 + Java使用
  10. java多线程学习笔记(三)