First, let's talk about the differences between dynamic proxy and static Proxy:
Static Proxy: code written in advance or automatically generated by a specific tool, and then compiled. Before running the program, the. Class file of the proxy class already exists.
Dynamic Proxy: the proxy is created dynamically using the reflection mechanism when the program runs. The proxy does not exist before the program runs.
Java static proxy is the simplest scenario. One interface and one implementation class can be done by one proxy class. This proxy class holds the reference of the interface implementation class, in this way, the delegate object can be enhanced.
Java provides two types of dynamic proxies: JDK dynamic proxies and cglib dynamic proxies.
Among them, JDK dynamic proxy mainly uses the interface invocationhandler and the proxy class.
Invocationhandler interface:
Public interface invocationhandler {
Public object invoke (Object proxy, method, object [] ARGs) throws throwable;
}
Parameter description:
Object Proxy: refers to the proxy object.
Method method: method to be called
Object [] ARGs: parameters required for method calling
Proxy class:
Proxy class is an operation class dedicated to proxy. You can use this class to dynamically generate proxy classes for one or more interface implementation classes. This class provides the following operation methods:
Public static object newproxyinstance (classloader loader, class <?> [] Interfaces, invocationhandler h) throws illegalargumentexception
Parameter description:
Classloader Loader: Class Loader
Class <?> [] Interfaces: Get all interfaces
Invocationhandler H: obtains the subclass instance of the invocationhandler interface.
The bytecode of the dynamic proxy class is dynamically generated by the Java reflection mechanism when the program is running, without the need for programmers to manually write its source code. Dynamic proxy not only simplifies programming, but also improves the scalability of the software system, because the Java reflection mechanism can generate any type of dynamic proxy class. The proxy class and invocationhandler interface in the Java. Lang. Reflect package provide the ability to generate dynamic proxy classes. Let's take a look at a small example ~
package go.derek;interface Istudy{public void study(String words);}
The preceding interface defines a method. The following is the implementation class of this interface.
package go.derek;class StudyImpl implements Istudy{public void study(String words){System.out.println(words);}}
Let's take a look at the proxy code.
package go.derek;import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; class StudyProxy implements InvocationHandler{private Object target;public Object bind(Object target){this.target=target;return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this); }public Object invoke(Object proxy,Method method,Object[] args) throws Throwable{System.out.println("start");Object obj=method.invoke(target,args);System.out.println("rest");return obj;}}The following is the code of the test class.
package go.derek;class ProxyTest {public static void main(String[] args) {StudyProxy sp=new StudyProxy();Istudy derek=(Istudy)sp.bind(new StudyImpl());derek.study("I'm derek");}}
The output result is as follows:
Start
I'm Derek
Rest
We can see that the proxy class is indeed enhanced for the delegate class, but it cannot be ignored that the proxy class must hold the reference of the delegate class object.
JDK dynamic proxy