spring aop的前奏,动态代理 (5)
目录
一、先看一个计算器的抽取和实现
- 图中,有两个需求
- 需求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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?