Dynamic Proxy Classes
The Java dynamic proxy class is located under the Java.lang.reflect package, which generally involves the following two classes:
1.Interface Invocationhandler
Only one method is defined in the interface:
Object Invoke (Object proxy, Method method, object[] args)
When actually used, the first parameter, obj, generally refers to the proxy class, which method is the proxy, as in the previous example, request (), args is the parameter array for the method (NULL when no parameter is set).
This abstract method is implemented dynamically in the proxy class.
2.Proxy
The class is a dynamic proxy class that acts like the proxysubject in the example above, which mainly includes the following:
Protected Proxy (Invocationhandler H): constructor, used to assign values to internal invocation handler.
Static class<?> Getproxyclass (ClassLoader loader, class<?> .... Interfaces): Loader is a class loader, Interfaces is an array of all the interfaces owned by the real class.
Static Object newproxyinstance (ClassLoader loader, class<?>[] interfaces, Invocationhandler h): Returns an instance of the proxy class, The returned proxy class can be used as a proxy class (you can use the method declared by the proxy class in the subject interface).
Dynamic proxy class description
The so-called dynamic Proxy is a class:
It is the class generated at runtime and you must provide a set of interface to it when it is generated, and then the class declares that it implements these interface.
You can certainly use this class instance as any of these interface.
Of course, this dynamic proxy is actually a proxy, it will not do a substantial job for you, in the generation of its instance you must provide a handler, it takes over the actual work.
When using dynamic proxy classes, we must implement the Invocationhandler interface. Each dynamic proxy class will have a invocation handler associated with it.
The real call is done in the Invoke () method of the invocation handler.
Example Description:
First define the abstract role and the real role class:
Interface subject{ void request ();
Implements subject{ @Override void request () {System.out.println ("from Real subject!" ); }}
Then define a Dynamicsubject class:
ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;/*** The internal property of the proxy class is the object type, which is passed in by the constructor method of the class when it is actually used. * This class implements the Invoke () method, in which Method.invoke () is actually the method to invoke the object being proxied, and the method parameter sub indicates that the method belongs to a sub. * With dynamic proxy classes, we can add some of our own additional methods before and after the execution of real-world object methods**/PublicClass DynamicsubjectImplementsinvocationhandler{//A reference to a real objectprivate Object Sub; public Dynamicsubject (Object obj) {this.sub = obj;} @Override public object Invoke (object proxy, method, object[] args) throws Throwable {System.out.println ("before calling:" + method); // Invoke method by reflection Method.invoke (sub, args); System.out.println ("After calling:" + method); return null;}}
When used:
ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Proxy;PublicClassclient{PublicStaticvoidMain (string[] args) {Realsubject Realsubject =NewRealsubject (); Invocationhandler handler =new Dynamicsubject (realsubject); class<?> ClassType = Handler.getclass (); // build agent // dynamically generates a class (implements the specified interface), generates the class object, translates to interface type Subject Subject = (Subject) Proxy.newproxyinstance (ClassType. getClassLoader (), Realsubject.getclass (). Getinterfaces (), handler); Subject.request (); // When the method is called, it is transferred to the handler takeover by the Invoke () method where the actual completion of the method is performed System.out.println (Subject.getclass ()); // print out: Class $Proxy 0 //< Span style= "COLOR: #008000" > $Proxy 0 is a class that is dynamically generated during runtime }}
In this way, the Proxied object (Realsubject) can be dynamically changed at runtime, the interface that needs to be controlled (subject interface) can be changed at runtime, the control mode (Dynamicsubject Class) can also be changed dynamically, thus realizing a very flexible dynamic agent relationship.
Dynamic Agent
A dynamic proxy is a method by which a customer invokes another object through a proxy class.
Dynamic Proxy usage Scenarios:
Debugging.
Remote Method Invocation (RMI).
Dynamic Agent Steps
1. Create a class that implements the interface Invocationhandler, which must implement the Invoke () method.
2. Create the class and interface that is being proxied.
3. Create a proxy through the static method of proxy newproxyinstance (ClassLoader loader, class<?>[] interfaces, Invocationhandler h).
4. Invoke the method through the proxy.
Dynamic Proxy Implementation Example 2:
Dynamic Proxy Mode instance 2
Dynamic Proxy Implementation Example 3:
In this example, an interface is defined:
Interface foo{ void doAction ();}
This interface has two implementation classes:
Implements foo{ @Override void doAction () {System.out.println ("from Implement 1!" ); }}implementsvoid doAction () {System.out.println ("from Implement 2!") ); }}
Defines the invocation handler, where the set method makes the actual object replaceable:
ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;PublicClass CommoninvocationhandlerImplementsinvocationhandler{Private Object target; public Commoninvocationhandler () {} public Commoninvocationhandler (Object obj) { This.target = obj;} public void Settarget ( Object target) {this.target = target;} @Override publicthrows Throwable {return< Span style= "COLOR: #000000" > Method.invoke (target, args); }}
Use:
ImportJava.lang.reflect.Proxy;PublicClassdemo{Publicstatic void main (string[] args) {Commoninvocationhandler handler = new Commoninvocationhandler (); Foo f = nullnew FOOIMPL1 ()); f = (Foo) proxy.newproxyinstance (foo.< Span style= "COLOR: #0000ff" >class.getclassloader (), new Class[] {foo. Class}, handler); F.doaction (); System.out.println ("----------------------------" new FOOIMPL2 ()); F.doaction (); }}
After running the program output:
From Implement 1!
----------------------------
From Implement 2!
Java proxy Mode (ii) dynamic Proxy in Java