前段时间学习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]
请各位帮忙答疑!
谢谢!
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]
请各位帮忙答疑!
谢谢!