On the Internet to read some information about dynamic agents and well-written blog post, and then, in order to deepen the impression, they are more than the cat painting Tiger Understanding.
Java dynamic agent, is relative to the static agent, is to solve the interface caused by multiple methods of maintenance of the product.
The main implementation, is by the Invocationhandler interface and proxy agent class to complete the ground.
Code structure, is the way, interface Impluserdao + Interface implementation Class Userdaoimpl + proxy class userdaoproxy+ test class client.
The proxy object ImplUserDao2 is created in the test class, and the proxy object ImplUserDao2 through handler in the agent Invocationhandler to find the proxy class Userdaoproxy and implement the dynamic proxy through the Invoke () method. Basically, it's something. ImplUserDao2 find the director of the intermediary platform Invocationhandler handler to invoke Invoke (), the indirect completion of the ground.
1 Public Interface Impluserdao {2 Public void print (); // defining interface Printing Methods 3 }
Impluserdao Interface
1 Public classUserdaoimplImplementsimpluserdao{2 3 /*(Non-javadoc)4 * @see cn.fupin.dao.impluserdao#print ()5 */6 Public voidprint () {7System.out.println ("Userdaoimpl.print () I was printed ...");8 9 }Ten One}Userdaoimpl Implementation Class
1 Public classUserdaoproxyImplementsinvocationhandler{2 3 PrivateImpluserdao Impluserdao;//define a real object to be proxied4 5 //create a constructor method to invoke the proxy class Shishun6 Publicuserdaoproxy (Impluserdao Impluserdao) {7 8 This. Impluserdao =Impluserdao;9 }Ten One A PublicObject Invoke (Object proxy, Method method, object[] args)throwsThrowable { - //when a proxy object invokes a real object, it is automatically associated to the Invocationhandler object to implement the dynamic proxy. - Method.invoke (Impluserdao, args); the return NULL; - } - -}Userdaoproxy proxy class
1 Public classClient {2 Public Static voidMain (string[] args) {3 //the real object of the agent4Impluserdao Impluserdao =NewUserdaoimpl ();5 //creates a proxy interface object. Without this handler object, there is no feeling of looking for someone, what about the agent6 //The principal interface of the agent Invocationhandler, when creating a proxy object, is automatically associated to the handler object to invoke the Invoke () method in the proxy class to implement the proxy. 7Invocationhandler handler =NewUserdaoproxy (Impluserdao);8 9 //Create a proxy objectTenImpluserdao ImplUserDao2 =(Impluserdao) proxy.newproxyinstance (Handler.getclass (). getClassLoader (), One Impluserdao.getclass (). Getinterfaces (), A handler); - - impluserdao2.print (); the } -}Client Test class
Invoke () method parsing
public object invoke (object proxy, Method method, object[] args) throws Throwable {
When a proxy object invokes a method of a real object, it automatically jumps to the Invoke method of the handler object associated with the proxy object to invoke
Method.invoke (subject, args);
return null;
}
Parameter proxy, which defines the real object we're going to represent.
Parameter method, which defines some of the ways we want to invoke real objects
Parameter args, which defines the parameters that are accepted when a method of a real object is called
Method parsing:
Static Object newproxyinstance (ClassLoader loader, class<?>[] interfaces, Invocationhandler h)
Parameter loader, a ClassLoader object that defines which ClassLoader object to load on the generated proxy object
parameter interfaces, an array of interface objects, represents a set of interfaces that will be given to the object that needs the proxy, and if I provide a set of interfaces to the word, then the proxy object declares that the interface is implemented so that I can invoke the methods in this set of interfaces.
The parameter H, the Invocationhandler object, needs to be associated with the object that invokes the Invoke () method when the proxy object invokes the real object.
The principal interface of the agent Invocationhandler, when creating a proxy object, is automatically associated to the handler object to invoke the Invoke () method in the proxy class to implement the proxy.
Invocationhandler handler = new Userdaoproxy (Impluserdao);
Create a proxy object
Impluserdao ImplUserDao2 = (Impluserdao) proxy.newproxyinstance (Handler.getclass (). getClassLoader (),
Impluserdao.getclass (). Getinterfaces (),
handler);
Quoted from: http://www.cnblogs.com/xiaoluo501395377/p/3383130.html
Implementation of dynamic Agent in Java