In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
The previous article explained the basic concept of agent and static agent mechanism: the agent pattern of design pattern.
Now let's talk about the dynamic proxy mechanism of JAVA.
There is an important interface invocationhandler and an important class Proxy in java's dynamic proxy mechanism. Let's take a look at the official documentation:
InvocationHandler is the interface implemented by the invocation handler of a proxy instance.Each proxy instance has an associated invocation handler. When a method is invoked on a proxy instance, the method invocation is encoded and dispatched to the invoke method of its invocation handler.
The call handler of the proxy instance needs to implement the InvocationHandler interface.
Each proxy instance is associated with a call handler. When a method is called by a proxy instance, the call to the method is encrypted and distributed to the calling method of the caller.
The translation is a mouthful, it doesn't matter, the generation will see the execution process of the code.
This call handler has a calling method:
Object invoke (Object proxy, Method method, Object [] args) throws Throwable
Handles the calling method of the proxy object and returns the result. When the caller bound by the proxy object is called, this method is called by the caller.
Proxy the proxy object of the called method
The method proxy object calls the corresponding method of the interface method.
Args invokes the array of objects passed in by the method through the proxy object interface. The interface method is empty if it has no parameters.
Let's take a look at the documentation description of proxy:
Public class Proxy extends Object implements SerializableProxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods.
Proxy provides static methods to create dynamic proxy classes and objects, and is also a superclass for these methods to create proxy objects.
A dynamic proxy class is a class that implements a series of methods that specify interfaces at run time. The proxy method implements the proxy interface, the proxy class creates the proxy object, and each proxy class is associated with a calling program that implements the InvocationcationHandler interface. The calling method of the proxy object is distributed to the invoke method of the instance calling program through its proxy interface. Through the reflection mechanism, the specified method is called and passed in an array of objects.
Some properties of Proxy:
The Proxy class is public,final, and non-abstract
There is no need to specify a legal name for the proxy class, the class name will start with "$Proxy"
The proxy class needs to inherit Proxy
The proxy class needs to implement the interfaces specified at the time of creation in order.
If the proxy class implements a non-public interface, it needs to be defined under the interface's package of the same name.
Because the proxy class implements all the specified interfaces when it is created, calling getInterfaces on the class object will return an array of interface lists, and calling getMetjods will return an array that implements all interface methods. Calling getMethod will return the method expected by proxy.
Proxy.isProxyClass judges whether it is an agent method or not.
The java.security.ProtectionDomain of the proxy class, like the system class, is loaded in the started class
Each proxy class has a constructor that implements the InvocationHandler interface and binds the caller to the proxy object. Instead of getting the constructor through the reflection mechanism, the proxy object can construct the proxy object by calling the Proxy.newProxyInstance method.
Properties of the proxy interface
The proxy class implements the proxy interface
Each proxy instance is associated with a caller through a constructor. The static method Proxy.getInvocationHandler will return a caller specified by the proxy object
The interface method call of the proxy object will be encrypted and distributed to the invoke method of the calling handler.
Calls to hasCode,equals,toString and other methods are encrypted and distributed to the calling program's invoke method, and interface methods are similarly encrypted and distributed.
Without saying much, the code is presented:
First, define an interface:
Package com.shadow.proxy.dynamicproxy;/** @ author sunny * / public interface ITask {public void setData (String data); public int getCalData (int x);}
Define a real theme that implements the interface business:
Package com.shadow.proxy.dynamicproxy;/** @ author sunny * / public class TaskImpl implements ITask {@ Override public void setData (String data) {System.out.println (data + "data is saved");} @ Override public int getCalData (int x) {return x * 10;}}
The most critical and core definition proxy class:
Package com.shadow.proxy.dynamicproxy;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;/** @ author sunny * / public class DynamicProxy implements InvocationHandler {private Object obj; public DynamicProxy (Object obj) {this.obj = obj;} @ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {Object result; System.out.println ("start execution method:" + method) Result = method.invoke (obj, args); System.out.println (method + "method execution ends"); return result;}}
Write a test file:
Package com.shadow.proxy.dynamicproxy;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Proxy;/** @ author sunny * / public class TestProxy {public static void main (String [] args) {ITask realTask = new TaskImpl (); InvocationHandler handler = new DynamicProxy (realTask); ITask proxyTask = (ITask) Proxy.newProxyInstance (realTask.getClass () .getClassLoader (), realTask.getClass () .getInterfaces (), handler) ProxyTask.setData ("have a hamburger"); System.out.println (proxyTask.getCalData (10));}}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.