/**
* Blind Date interface
*
* @author zhengt
* @time June 3, 2095 3:13:03 PM
*/
Public Interface Xiangqininterface {
/**
* Method of Blind Date
*/
Public void xiangqin ();
}
/**
* Zhang San Blind Date realization class
*
* @author zhengt
* @time June 3, 2095 3:14:48 PM
*/
Public class Zhangsanxiangqininterfaceimpl implements Xiangqininterface {
Public void Xiangqin () {
System.out.println ("Zhang San to a blind date, marry a beautiful wife." ");
}
}
import Java.lang.reflect.InvocationHandler;
import Java.lang.reflect.Method;
/**
* Blind Date is a lifelong event, before a blind date to prepare, dressed up handsome.
*
* @author zhengt
* @time June 3, 2095 3:15:48 PM
*/
Public class Readyinvocationhandler implements Invocationhandler {
///Blind Date interface implementation class, that is, Zhang San Blind Date class
Private Object Zhangsan = null;
Public Readyinvocationhandler (Object realsubject) {
this. Zhangsan = Realsubject;
}
Public Object Invoke (object proxy, Method m, object[] args) {
Object result = null;
Try {
/**
* Dynamic proxy class $proxy0 calls its own Xiangqin method when calling the Xiangqin method,
* and its own Xiangqin method is called the Super.h.invoke (this,), that is, the parent proxy of the H invoke method,
* This is the Invoke method of the Readyinvocationhandler class.
* So, the proxy of Invoke (Object Proxy, Method m, object[] args) is actually a dynamic proxy class $proxy0,
* If you turn it into Xiangqininterface and then call its Xiangqin method, then it will call Super.h.invoke (this,,), so it will die loop.
*/
/**
* Online about the most problem here is the object proxy put here to do what? I don't know about this, either.
* But at least we know what it is, and what it does, it's unclear.
*/
System.out.println (Proxy.getclass (). Getsimplename ());
System.out.println ("Zhang San before the blind date, the agent dressed him up." ");
result = M.invoke (Zhangsan, args);
} catch (Exception ex) {
System.exit (1);
}
return result;
}
}
import Java.lang.reflect.InvocationHandler;
import Java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* Zhang San came to the dating agency (blind Spot) and began a blind date.
*
* @author zhengt
* @time June 3, 2095 3:17:16 PM
*/
Public class Hunjiesuo {
Public Static void Main (String args[]) {
///Zhang San a blind date the implementation class instantiation of the matchmaking, that is, to get an instance object of the Xiangqininterface interface
Xiangqininterface Zhangsan = new Zhangsanxiangqininterfaceimpl ();
/**
* Get Zhangsanxiangqininterfaceimpl a proxy class for this class, and bind a handler class Readyinvocationhandler for the proxy class.
* Listen very much to the mouth, in fact, every time you call Zhangsanxiangqininterfaceimpl this subclass of the Xiangqin method,
* Not Zhangsan This instance of the Zhangsanxiangqininterfaceimpl class to invoke,
* But this Zhangsanxiangqininterfaceimpl proxy class Readyinvocationhandler to invoke its own Invoke method,
* In this invoke method, you can call the Xiangqin method of this instance of Zhangsan
*/
/**
* How to implement dynamic Proxy in Java?
* The first step, we have an interface, there is an interface implementation class, and this implementation class is the object we want to proxy,
* The so-called Proxy is called the implementation of the method of the class, you can do before and after the method execution of extra work, this is the proxy.
* The second step is to write ourselves a class that can do extra work when the method of the proxy class is executed, and this class must inherit the Invocationhandler interface.
* Why do you inherit it? Because an instance of the proxy class does not tune the actual implementation of this method of the class when invoking the method of implementing the class,
instead, call the Invoke method of the class (the method that must be implemented when inheriting), in which you can invoke the method of the real implementation class.
* The third step, when you want to use the proxy class instance to invoke the implementation of the method of the class, write down the following two pieces of code.
*/
Xiangqininterface proxy = (xiangqininterface) proxy.newproxyinstance (
Zhangsan.getclass (). getClassLoader (),
Zhangsan.getclass (). Getinterfaces (),
New Readyinvocationhandler (Zhangsan));
Proxy.xiangqin ();
/**
* Here to explain the meaning of the long-length code in the middle, and what exactly did it work?
* First, according to Zhangsan.getclass (). getClassLoader () This class loader to proxy classes and
* Zhangsan.getclass (). Getinterfaces () All interfaces to be implemented by the proxy class
* Call Proxy.getproxyclass as a parameter (ClassLoader loader, class<?> .... interfaces)
the * method returns the Java.lang.Class object of the proxy class, which is the class object that the Java dynamically generated proxy class $proxy0.
* At the same time, Java also allows the dynamically generated $PROXY0 class to implement all interfaces for the implementation of the proxy class, and inherits the proxy interface.
* Second, instantiate an instance of the dynamically generated $proxy0 class, and instantiate the proxy class's constructor as a proxy (Invocationhandler h),
* That is, to instantiate the dynamically generated $proxy0 class, you must give it a invocationhandler parameter, which we implement ourselves to use in the proxy class
* The class Readyinvocationhandler to do extra work before and after the method is performed.
* This Code proxy.newproxyinstance (Zhangsan.getclass (). getClassLoader (), Zhangsan.getclass (). Getinterfaces (), New Re Adyinvocationhandler (Zhangsan))
* Get is actually a class called $proxy0 extends Proxy implements Xiangqininterface class.
* Third, the $proxy0 class is forced into the xiangqininterface type, calling the Xiangqin method.
*/
}
}
Online to see a cow people read this dynamic agent source code, now the Web site to stay here, I hope to help friends.
Http://hi.baidu.com/malecu/blog/item/45d4952b31bc0e27d52af17a.html
It's 0:21 and I'm smoking and sleeping.