最近没事,看了一下Spring的书籍,从它最开始的AOP1到AOP2,于是对基本的实现原理有了一种探究的想法,AOP1用的Dynamic Proxy的模式是从1.3引入,其本质就是生成代理类,包装原有的对象,将原有对象的方法指派给包装后生成的代理类,在方法调用前后,甚至于方法调用上做手脚,这个手脚就要具体看是什么业务逻辑。
Proxy的代码很简单,核心就是newProxyInstance,接收参数为类加载器,接口,包装的InvokecationHandler,代码如下:
1 /*2 * Look up or generate the designated proxy class.
3 */
4 Class<?> cl = getProxyClass(loader, interfaces);
5
6 /*
7 * Invoke its constructor with the designated invocation handler.
8 */
9 try {
10 Constructor cons = cl.getConstructor(constructorParams);
11 return cons.newInstance(new Object[] { h });
12 } catch (NoSuchMethodException e) {
13 throw new InternalError(e.toString());
14 } catch (IllegalAccessException e) {
15 throw new InternalError(e.toString());
16 } catch (InstantiationException e) {
17 throw new InternalError(e.toString());
18 } catch (InvocationTargetException e) {
19 throw new InternalError(e.toString());
20 }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)