Java Proxy InvocationHandler invoke理解
来源:互联网 发布:switch网络一会b一会f 编辑:程序博客网 时间:2024/05/16 14:46
原文地址:点击打开链接
一.相关类及其方法:
java.lang.reflect.Proxy,
Proxy 提供用于创建动态代理类和实例的静态方法.
newProxyInstance()
Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler. This method is equivalent to: Proxy.getProxyClass(loader, interfaces). getConstructor(new Class[] { InvocationHandler.class }). newInstance(new Object[] { handler }); Proxy.newProxyInstance throws IllegalArgumentException for the same reasons that Proxy.getProxyClass does.返回一个指定接口的代理类实例,该接口可以将方法调用指派到指定的调用处理程序。
java.lang.reflect.InvocationHandler,
InvocationHandler 是代理实例的调用处理程序 实现的接口。
invoke()
在代理实例上处理方法调用并返回结果。在与方法关联的代理实例上调用方法时,将在调用处理程序上调用此方法。
二.源代码:
被代理对象的接口及实现类:
public interface Manager {public void modify();}
//接口实现类public class ManagerImpl implements Manager {@Overridepublic void modify() {System.out.println("*******modify()方法被调用");}}业务代理类
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;public class BusinessHandler implements InvocationHandler {private Object object = null;public BusinessHandler(Object object) {this.object = object;}@Overridepublic Object invoke(Object proxy, Method method, Object[] args)throws Throwable {System.out.println("do something before method");Object ret = method.invoke(this.object, args);System.out.println("do something after method");return ret;}}客户端类
import java.lang.reflect.Proxy;public class Client {public static void main(String[] args) {// 元对象(被代理对象)ManagerImpl managerImpl = new ManagerImpl();// 业务代理类BusinessHandler securityHandler = new BusinessHandler(managerImpl);// 获得代理类($Proxy0 extends Proxy implements Manager)的实例.Manager managerProxy = (Manager) Proxy.newProxyInstance(managerImpl.getClass().getClassLoader(), managerImpl.getClass().getInterfaces(), securityHandler);managerProxy.modify();}}三.执行结果:
do something before method*******modify()方法被调用do something after method
四.机制分析:
Proxy.(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)做了以下几件事.
(1)根据参数loader和interfaces调用方法 getProxyClass(loader, interfaces)创建代理类$Proxy.
$Proxy0类实现了interfaces的接口,并继承了Proxy类.
(2)实例化$Proxy0并在构造方法中把BusinessHandler传过去,接着$Proxy0调用父类Proxy的构造器,为h赋值,如下:
class Proxy{
InvocationHandler h=null;
protected Proxy(InvocationHandler h) {
this.h = h;
}
...
}
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.lang.reflect.UndeclaredThrowableException;public final class $Proxy0 extends Proxy implements Manager {private static Method m1;private static Method m0;private static Method m3;private static Method m2;static {try {m1 = Class.forName("java.lang.Object").getMethod("equals",new Class[] { Class.forName("java.lang.Object") });m0 = Class.forName("java.lang.Object").getMethod("hashCode",new Class[0]);m3 = Class.forName("com.ml.test.Manager").getMethod("modify",new Class[0]);m2 = Class.forName("java.lang.Object").getMethod("toString",new Class[0]);} catch (NoSuchMethodException nosuchmethodexception) {throw new NoSuchMethodError(nosuchmethodexception.getMessage());} catch (ClassNotFoundException classnotfoundexception) {throw new NoClassDefFoundError(classnotfoundexception.getMessage());}}public $Proxy0(InvocationHandler invocationhandler) {super(invocationhandler);}@Overridepublic final boolean equals(Object obj) {try {return ((Boolean) super.h.invoke(this, m1, new Object[] { obj })).booleanValue();} catch (Throwable throwable) {throw new UndeclaredThrowableException(throwable);}}@Overridepublic final int hashCode() {try {return ((Integer) super.h.invoke(this, m0, null)).intValue();} catch (Throwable throwable) {throw new UndeclaredThrowableException(throwable);}}public final void modify() {try {super.h.invoke(this, m3, null);return;} catch (Error e) {} catch (Throwable throwable) {throw new UndeclaredThrowableException(throwable);}}@Overridepublic final String toString() {try {return (String) super.h.invoke(this, m2, null);} catch (Throwable throwable) {throw new UndeclaredThrowableException(throwable);}}}接着把得到的$Proxy0实例强制转换成Manager.
当执行managerProxy.modify()方法时,就调用了$Proxy0类中的modify()方法.
在modify方法中,调用父类Proxy中的h的invoke()方法.
即InvocationHandler.invoke();
0 0
- Java Proxy InvocationHandler invoke理解
- java动态代理-InvocationHandler Proxy
- Java动态代理InvocationHandler+Proxy
- 初探java的Proxy+InvocationHandler
- java 动态代理学习(Proxy,InvocationHandler)——自己的理解
- java 动态代理学习(Proxy,InvocationHandler)——自己的理解
- Spring AOP之 java 动态代理(Proxy 与 InvocationHandler)理解
- 从java 反射看延迟加载(代理模式)(五)——理解Proxy、InvocationHandler
- java 动态代理学习(Proxy,InvocationHandler)
- java 动态代理深度学习(Proxy,InvocationHandler)
- java 动态代理学习(Proxy,InvocationHandler)
- JAVA动态代理实现 Proxy InvocationHandler
- JAVA JDK 动态代理 proxy invocationHandler
- java 动态代理深度学习(Proxy,InvocationHandler)
- java proxy InvocationHandler 模拟 spring aop
- Java 动态代理(proxy、invocationHandler)
- Java动态代理用法(Proxy + InvocationHandler)
- java 动态代理学习(Proxy,InvocationHandler)
- 张宏民告别《新闻联播》为何一声叹息
- River Hopscotch - POJ 3258 二分
- 优惠券制作和分配(含代码)
- openstreetmap我觉得会火
- UML 类图
- Java Proxy InvocationHandler invoke理解
- MFC下unsigned char和Cstring的转换
- 智能手表开发API接口之我见
- 如何在VS 编译器下 使用纯 C 语言编译器?
- Junit 入门(二)
- [LeetCode]Partition List
- ComboBox消息打开列表
- 安卓实训第九天---Activity的复习以及在Onstart里设置网络连接
- 没有方向了?