The following content is partially based on content on the Internet. I would like to express my gratitude to the original author!
The key to realizing dynamic proxy in Java is these two things: Proxy and InvocationHandler. Let’s start with the invoke method in the InvocationHandler interface and briefly explain how Java implements dynamic proxy. N i First, the integrity of the Invoke method is as follows: java code
method.invoke(obj, args);
returnA method invocation on a proxy instance through one of its pr oxy interfaces will be dispatched to the invoke method of the instance’s invocation handler, passing the proxy instance, a java.lang.reflect.Method object identifying the method that was invoked, and an array of type Object containing the arguments.
For the convenience of explanation, here is a simple example to implement dynamic proxy.
public interface Subject {
}
//Real role: Implemented the Subject’s request() method
public class RealSubject implements Subject{
public void request(){
System.out.println("From real subject.");
}
}
Java code
public
{
private Object obj;
this.obj = ob j;
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable{
System.out.println( "before calling " + method);
null;
}
)
//Client: generated a proxy instance and called the request() method
public class Client {
public static void main(String [] args) throws Throwable{
Subject rs=new RealSubject();//Specify the proxy here Class
Class> cls=rs.getClass();
Interfaces(), ds);
instanceof Proxy);
//Here It can be seen that the subject's Class class is $Proxy0. This $Proxy0 class inherits Proxy and implements the Subject interface.
toString());
System.out.print("The attributes in subject are: "); [] field=subject.getClass() .getDeclaredFields();
"subject’s parent class is: "+subject.getClass().getSuperclass());
System. out.print(
"n"+Class>[] interfaces=subject.getClass().getInterfaces();
}
System.out.println("nn"+"The operation result is: "); Subject.request();
true subject’s Class class is: class $Proxy0 The attributes in
subject are: m1, m3, m0, m2, methods in subject are: request , hashCode, equals, toString,
Java code public static Object newProxyInstance(ClassLoader loader, Class>[] interfaces, InvocationHandler h) throws IllegalArgumentException { if (h == null) { throw new NullPointerException(); } /* * Look up or generate the designated proxy class. */ Class cl = getProxyClass(loader, interfaces); /* * Invoke its constructor with the designated invocation handler. */ try { /* * Proxy源码开始有这样的定义: * private final static Class[] constructorParams = { InvocationHandler.class }; * cons即是形参为InvocationHandler类型的构造方法 */ Constructor cons = cl.getConstructor(constructorParams); return (Object) cons.newInstance(new Object[] { h }); } catch (NoSuchMethodException e) { throw new InternalError(e.toString()); } catch (IllegalAccessException e) { throw new InternalError(e.toString()); } catch (InstantiationException e) { throw new InternalError(e.toString()); } catch (InvocationTargetException e) { throw new InternalError(e.toString()); } } class Proxy{ InvocationHandler h=null; protected Proxy(InvocationHandler h) { } ... } Let’s take a look at the source code of $Proxy0 which inherits Proxy:
Proxy.newProxyInstance(ClassLoader loader, Class>[] interfaces, InvocationHandler h) does the following things.
(1) According to the parameters loader and interfaces calling method getProxyClass(loader, interfaces) creates the proxy class $Proxy0. The $Proxy0 class implements the interfaces interface and inherits the Proxy class.
(2) Instantiate $Proxy0 and pass the DynamicSubject in the constructor, and then call $Proxy0 The constructor of the parent class Proxy assigns a value to h as follows:
Java code
The above is the detailed content of Tutorial on implementing dynamic proxy in Java. For more information, please follow other related articles on the PHP Chinese website!