Proxy mode uses the proxy object to complete the user's request, masking the user's access to the real object.
Agent mode uses a lot, for example, for security reasons, it is necessary to mask the client directly access the real object, or in the remote call, the use of proxy objects to deal with the technical details of the remote method, or to enhance the system, the real object encapsulation, so as to achieve the purpose of delay loading.
When the system is started, the most resource-consuming method is separated by proxy mode, which can speed up the system's startup speed and reduce the user's waiting time. In the user is actually doing the query is, then by the proxy class loading the real class, complete the user request. This is the purpose of using proxy mode to achieve deferred loading.
1. Static Proxy implementation:
Subject interface:
Public interface Idbquery {
String request ();
}
Real theme:
public class Dbquery implements Idbquery {public
dbquery () {
try {
thread.sleep (10000);
} catch ( Exception e) {
e.printstacktrace ();
}
}
public string request () {return
"String request";
}
}
Agent class:
public class Idbqueryproxy implements Idbquery {
private dbquery dbquery;
Public String request () {
if (dbquery==null)
dbquery = new Dbquery ();
return Dbquery.request ();
}
Finally, the main function:
public class Proxytext {public
static void Main (string[] args) {
idbquery dbquery = new Idbqueryproxy ();
System.out.println (Dbquery.request ());
}
Static agent note, the proxy class is a real class implementation of the common interface, and the proxy class refers to the real class object, the time-consuming operation in the proxy class method implementation.
Dynamic Agent:
The dynamic proxy class is generated dynamically when it is run. That is, the byte code of the proxy class is generated at run time and loaded into the current classloader. Compared with static agents, dynamic agents do not need to be real attention to encapsulate a form exactly the same package class, if the theme interface a lot, it is necessary for each interface to write an agent method is very annoying, if the interface has changed, the real class and proxy classes need to change, which is not conducive to system maintenance; The use of some dynamic proxy generation methods can even be run to specify the execution logic of the proxy class, thereby greatly increasing the flexibility of the system.
Subject interface:
Public interface Idbquery {
String request ();
}
JDK proxy class:
public class Jdbdbqueryhandler implements invocationhandler{
idbquery idbquery = null;
@Override Public
object Invoke (Object proxy, Method method, object[] args)
throws Throwable {
if (idbquery== NULL) {
idbquery = new Dbquery ();
}
return Idbquery.request ();
}
public static Idbquery Createjdbproxy () {
idbquery jdkproxy = (idbquery) proxy.newproxyinstance ( Classloader.getsystemclassloader (),
new Class[]{idbquery.class}, New Jdbdbqueryhandler ());
System.out.println ("Jdbdbqueryhandler.createjdbproxy ()");
Return Jdkproxy
}
}
Main function:
public class Proxytext {public
static void Main (string[] args) {
Idbquery idbquery = Jdbdbqueryhandler.createj Dbproxy ();
System.out.println (Idbquery.request ());
}
Alternatively, you can use the Cglib and javassist dynamic proxies to resemble JDK dynamic proxies, but the JDK dynamic class is created the fastest because the Difineclass () method of this built-in implementation is defined as native implementation, so performance is superior to the others. In the proxy class function call, the JDK dynamic proxy is inferior to the cglib and javassist dynamic proxy, but the javassist dynamic proxy performance quality is the worst, even inferior to the JDK realization. In the actual development application, the proxy class method calls the frequency to be far above the proxy class actual generation frequency, therefore the dynamic proxy method invocation performance should become the performance attention point. The JDK dynamic proxy enforces that the proxy class and the true theme implement the unified interface, Cglib and javassist dynamic proxies do not have such requirements.
In Java, the implementation of dynamic proxy involves the use of ClassLoader, taking Cglib as an example, briefly describes the loading process of dynamic classes. To generate a dynamic proxy using Cglib, you first need to generate an instance of the enhancer class and develop a callback class for handling the proxy business. In the Enhancer.create () method, the Defaultgeneratorstrategy.generate () method is used to generate the byte code of the proxy class and is saved in a byte array. Then call the Reflectutils.defineclass () method, through reflection, call the Classloader.defineclass () method, load the byte code into the ClassLoader, complete the class loading. Finally, a dynamic class instance is generated by reflection through the Reflectutils.newinstance () method, and the instance is returned. The other is different from the details of the process, but the generation logic is the same.
The above is the entire content of this article, I hope to help you learn.