动态代理之:接口InvocationHandler

前段时间学习JDBC过程中碰到动态代理的相关知识,做测试过程中不能理解接口InvocationHandler 中方法:public Object invoke(Object proxy, Method method, Object[] args) throws Throwable的参数Object proxy做何用,关于动态代理的相关知识就不在此赘述了,现将自己的例子附上,请大家看看参考。

1.接口:
public interface IUser {
public String getName();
}
2.接口对应的类:
public class UserImp implements IUser {
String name;
public UserImp(String name){
this.name = name;
}
public String getName() {
return name;
}

}
3.监听方法所在类,该类实现了InvocationHandler 接口,通过invoke方法利用java的反射机制实现对相关方法的监听。
public class Handler implements InvocationHandler {


public Object targetObj;


public Handler(Object targetObj) {
this.targetObj = targetObj;
}

[color=red]public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable [/color]{

System.out.println("before the function \""+method.getName()+"\"");
Object ret = method.invoke(targetObj, args);
System.out.println(ret);
System.out.println("after the function \""+method.getName()+"\"");

}

}
4.测试方法:
public class testMain {

public static void main(String[] args) {

IUser realUser = new UserImp("sun");
Handler hand = new Handler(realUser);
IUser proxy = (IUser) Proxy.newProxyInstance(realUser.getClass().getClassLoader(), realUser.getClass().getInterfaces(), hand);
proxy.getName();
}

}
5.输出结果:
before the function "getName"
sun
after the function "getName"


[color=red]疑问:[/color]在步骤3中[color=red]invoke(Object proxy, Method method, Object[] args)方法[/color]的参数proxy的作用是什么,如何使用?
在论坛中看到大家发帖讲述的该参数的作用为:proxy就是你调用的代理
java doc中的说明为:proxy - 在其上调用方法的代理实例

按照上面的叙述,是否可以将步骤3的代码改成如下的形式:
public class Handler implements InvocationHandler {

/*
public Object targetObj;


public Handler(Object targetObj) {
this.targetObj = targetObj;
}
*/

[color=red]public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable [/color]{

System.out.println("before the function \""+method.getName()+"\"");
Object ret = method.invoke(proxy, args);
System.out.println(ret);
System.out.println("after the function \""+method.getName()+"\"");

}

}

但是改成这样后程序好像进入了死循环一样,如下是输出结果:

[color=red]at Handler.invoke(Handler.java:27)
at $Proxy0.getName(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at Handler.invoke(Handler.java:27)
at $Proxy0.getName(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at Handler.invoke(Handler.java:27)[/color]

请各位帮忙答疑!
谢谢!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值