[Java] 13: proxy mode-dynamic proxy!
In the previous blog, we talked about a form of proxy mode-static proxy. At the end of this article, we mentioned the shortcomings of static proxy, one of them is that when there are many business classes, it is obviously a very bad choice to use static proxy. How can this problem be solved? This leads to our dynamic proxy.
What is dynamic proxy?
The so-called dynamic proxy is relative to the static proxy. The reflection mechanism is used to determine the class to be loaded during the runtime of the program, which effectively avoids the problem that a business class of static proxy corresponds to a proxy class.
Why dynamic proxy?
Dynamic proxy is used to solve the problem of poor scalability and difficult maintenance of static proxy. This is what I mentioned at the beginning of this article,
How to Use Dynamic proxy?
To use dynamic Proxy, you must use Proxy and InvocationHandler. Next I will use the example of the previous blog to show you how to use dynamic proxy.
The user-managed interfaces and implementations will not be described in detail. The following describes the dynamic proxy LogHandler and client code:
The dynamic proxy object class LogHandler that simulates the log function. The Code is as follows:
Public class LogHandler implements InvocationHandler {// target Object private Object targetObject;/*** instantiation method of this class, it is used to pass the target Object * @ param targetObject target Object * @ return */public Object newProxyInstance (Object targetObject) into this.tar getObject = targetObject; // call the static method newProxyInstance of the Proxy class to instantiate a Proxy class and pass the target object as a parameter to return Proxy. newProxyInstance (targetObject. getClass (). getClassLoader (), targetObject. getClass (). getInterfaces (), this);}/*** Method for executing the target Object */public Object invoke (Object proxy, method Method, Object [] args) throws Throwable {// print the System before the method of the target object is executed. out. println ("start execution method -->" + method. getName (); for (int I = 0; I
Client:
Public class Client {/*** @ param args */public static void main (String [] args) {// instantiate the dynamic proxy class LogHandlerLogHandler logHandler = new LogHandler (); // pass the user management implementation class into the dynamic proxy class UserManager userManager = (UserManager) logHandler. newProxyInstance (new UserManagerImpl (); // userManager. addUser ("0001", "Zhong Yuemin"); // userManager. delUser ("0001"); String name = userManager. findUser ("0001"); System. out. println ("Client. main () --- "+ name );}}
Summary:
The biggest advantage of dynamic proxy compared with static proxy is that all methods declared in the interface are transferred to the InvocationHandler. invoke method that calls the processor ). In this way, when there are a large number of interface methods, we can flexibly process them without the need to transfer each method like a static proxy. Moreover, the application of Dynamic proxy makes our class responsibilities more uniform and more reusable.
Whether it is static proxy or dynamic proxy, this idea of proxy is very instructive. Many frameworks and technologies have the shadows of proxies. For example, the essence of AOP in Spring is the proxy mode, and every framework in the SSH framework uses the proxy mode.