There are two types of proxies: one is the Java Dynamic proxy that implements the interface, and the other is the proxy that does not implement the interface based on cglib.
Proxy provides a static method for creating dynamic proxy classes and dynamic proxy objects. It is also the parent class of all dynamic proxies,
The following describes dynamic proxy through a simple example:
Dog:
Public interface dog {
Public void Info ();
Public void run ();
}
Dog implementation class:
Public class dogimpl implements dog {
Public void Info (){
System. Out. println ("I Am a hound ");
}
Public void run (){
System. Out. println ("I am running very fast ");
}
}
Public class dogutil {
Public void Method1 (){
System. Out. println ("=========== simulate the first common method =========== ");
}
Public void method2 (){
System. Out. println ("=========== simulate the second common method =========== ");
}
}
/**
* This class is an implementation class of invocationhandler. The invoke method of this class will be implemented as a proxy object.
*/
Public class myinvocationhandler implements invocationhandler {
/** Objects to be proxy */
Private object target;
Public void settarget (object target ){
This.tar get = target;
}
/** All methods for executing the dynamic proxy object will be replaced with the following invoke Method */
Public object invoke (Object proxy, method, object [] ARGs)
Throws throwable {
Dogutil = new dogutil ();
Dogutil. Method1 ();
Object result = method. Invoke (target, argS );
Dogutil. method2 ();
Return result;
}
}
/**
* A dynamic proxy instance is generated for the specified target.
*/
Public class myproxyfactory {
// Note that proxy provides two methods to create a dynamic proxy class and a dynamic proxy instance, for example:
Public static object getproxyinstance (object target ){
Myinvocationhandler handler = new myinvocationhandler ();
Handler. settarget (target );
/* Create and return the dynamic proxy */
Return proxy. newproxyinstance (target. getclass (). getclassloader (),
Target. getclass (). getinterfaces (), Handler );
}
Public static object getproxyclass (object target) throws exception {
Myinvocationhandler handler = new myinvocationhandler ();
Handler. settarget (target );
/* Create and return the dynamic proxy */
Class clazz = proxy. getproxyclass (target. getclass (). getclassloader (), target. getclass (). getinterfaces ());
Constructor con = clazz. getconstructor (new class [] {invocationhandler. Class });
Return con. newinstance (new object [] {handler });
}
}
Test:
Public class test {
Public static void main (string [] ARGs) throws exception {
Dog target = new dogimpl ();
// Dog = (DOG) myproxyfactory. getproxyclass (target );
Dog dog = (DOG) myproxyfactory. getproxyinstance (target );
Dog.info ();
Dog. Run ();
}
}
Output result:
=========== Simulate the first common method ===============
I am a hound
=========== Simulate the second common method ==============
=========== Simulate the first common method ===============
I ran very fast.
=========== Simulate the second common method ==============