1. What is a dynamic agent? Speaking of dynamic, it is better to first say what is static. The so-called static agent, created by programmers or specific tools to automatically generate source code, and then compile it. Before the program runs, the. class file for the proxy classes already exists. In short, it is the compiled proxy class that existed before the program was run.
Conversely, if the proxy class program does not exist before running, when the program is running, using the reflection mechanism is created dynamically (no need to manually write proxy class source code), that is today to say the dynamic agent.
2. Principle of realization
There are two classes related to Dynamic Agents
1), Interface Invocationhandler
Object Invoke (Object proxy, method, object[] args)
is used to represent more than n things we want to do.
2), class Proxy
A
class that truly represents a dynamic proxy, providing two static methods:
class<?> Getproxyclass (ClassLoader loader, class<?>[] interface)
used to generate the proxy class, parameters to provide a interface array, which will generate these interface "virtual implementation",
used to impersonate a real object.
Object newproxyinstance (ClassLoader loader, class<?>[] interfaces, Invocationhandler h)
generate proxy object, more Invocationhandler parameter (just the implementation class of Invocationhandler interface),
it is associated with the proxy object and automatically executes the H.invoke (...) when the request is distributed to the proxy object. Method
3), Agent diagram
3. Code Display
1) , Usermanager:
<span style= "FONT-SIZE:18PX;" > Public interface Usermanager {public void AddUser (String userid,string userName); public void Deluser (String userId); public void ModifyUser (String userid,string userName); public string Queryuser (string userId); </span>
2), Usermanagerimpl:
<span style= "FONT-SIZE:18PX;" > Public class Usermanagerimpl implements Usermanager { @Override public void AddUser (String userId, String userName) { System.out.println ("Usermanagerimpl.adduser ()"); } @Override public void Deluser (String userId) { System.out.println ("Usermanagerimpl.deluser ()"); } @Override public void ModifyUser (String userId, String userName) { System.out.println (" Usermanagerimpl.modifyuser () "); } @Override public string Queryuser (String userId) { System.out.println ("Usermanagerimpl.queryuser ()"); return "Zhangsan"; } } </span>
3), Usermanagerimpl dynamic agent:
<span style= "FONT-SIZE:18PX;" > public class Loghandler implements Invocationhandler {private Object targetObject; public Object Newproxyinstance (object targetObject) {this.targetobject=targetobject; Return Proxy.newproxyinstance (Targetobject.getclass (). getClassLoader (), Targetobject.getclass (). getInt Erfaces (), this); } @Override public object invoke (object proxy, Method method, object[] args) throws Throwable {System.out.println ("start-->>" + method.getname ()); for (int i=0;i<args.length;i++) {System.out.println (args[i]); } Object Ret=null; try{//Call target method Ret=method.invoke (TargetObject, args); System.out.println ("success-->>" + method.getname ()); }catch (Exception e) {e.printstacktrace (); System.out.println ("error-->> " + Method.getname ()); Throw e; } return ret; }}</span>
4), client invocation:
public static void Main (string[] args) { loghandler loghandler=new loghandler (); Usermanager usermanager= (Usermanager) loghandler.newproxyinstance (New Usermanagerimpl ()); Usermanager.adduser ("1", "Zhangsan"); String Username=usermanager.queryuser ("1"); System.out.println ("UserName:" +username); }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Introduction to Dynamic Agents