The concept of an agent
Dynamic Agent technology is the most important technology in the whole Java technology, it is the basis of Learning Java framework, not dynamic proxy technology, then learning spring these frameworks are not understood.
Dynamic Agent technology is the proxy object used to produce an object . Why do I need to generate proxy objects for an object in development?
Take a real-life example: singers or stars have their own brokers, the broker is their agent, when we need to find a star performance, we can not directly find the star, can only be a star agent. For example, Andy Lau in real life is very famous, will sing, will dance, will film, Andy Lau in no famous before, we can directly find him singing, dancing, filming, Andy Lau famous, he did the first thing is to find a broker, this agent is Andy Lau's agent (agent), When we need to find Andy Lau performance, can not directly find Andy Lau (Andy Lau said, you find my agent to discuss specific matters!), only to find Andy Lau's agent, so Andy Lau, the value of this agent is to intercept our direct access to Andy Lau!
This real-world example is the same as we do in development, where we create a proxy object for an object that is primarily used to intercept access to real business objects. So what should be the method of the proxy object? The proxy object should have the same method as the target object
So here are two concepts that explicitly represent the proxy object:
1. The value of proxy object is mainly used to intercept access to real business objects .
2. the proxy object should have the same method as the target object (the real business object) . Andy Lau (Real business object) will sing, Can Dance, will be filming, we can not directly find him singing, dancing, filming, can only find his agent ( agent ) singing, dancing, filming, a person to become Andy Lau's agent, Then he must have the same behavior as Andy Lau (can sing, Can Dance, will film), Andy Lau has what method, he (agent) will have what method, we find Andy Lau's agent singing, dancing, filming, but agents do not really know how to sing, dance, filming, really know how to sing, dance, filming is Andy Lau , in the real example is we want to find Andy Lau singing, dancing, filming, then can only find his agent, pay his agent, and then let Andy Lau to sing, dance, filming.
Second, the agent in Java 2.1, "
Java.lang.reflect.Proxy"Class Introduction
now to build a proxy object for an object, this proxy object is usually written with a class to generate , so first write the class used to generate the proxy object. In Java, how to use the program to generate a proxy object for an object, Java after JDK1.5 provides a "java.lang.reflect.Proxy" class, through the "Proxy" class provides a The Newproxyinstance method is used to create a proxy object for an object, as follows:
Newproxyinstance
The newproxyinstance method is used to return a proxy object, which has a total of 3 parameters, and ClassLoader loader is used to indicate which class loader the build proxy object uses, class<?>[] The interfaces is used to indicate which object is generated by the proxy object, specified by the interface, Invocationhandler h is used to indicate what the resulting proxy object will do. So we just need to call the newproxyinstance method to get a proxy object for an object.
2.2. Write the class that generates the proxy object
in Java, if you want to produce an object's proxy object, then this object must have an interface , so our first step is to design the interface of this object, and define the behavior of the object in the interface (method)
1. Define the object's behavior interface
/*** @ClassName person * @Description Agent Interface*/ Public Interfaceperson{/*** @Method sing * @Description singing *@paramname *@return */string Sing (string name); /*** @Mathod Dance * @Description dancing *@paramname *@return */string Dance (string name);}
2. Real object class (Andy Lau Real Class)
/** * @ClassName Liudehua * @Desrition Andy Lau realizes the person class realizes singing, dancing behavior */ public Span style= "COLOR: #0000ff" >class liudehua implements person{@Override public String s ing (String name) {System.out.println ( "Andy Lau Sings" +name+ "song" return "finished singing" ; @Override public string Dance (string name {System.out.println ( Andy Lau Jumps "+name+" W Dance " return "Dance skipped" ; }}
3. Agent Class (Andy Lau Agent)
ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy;/*** @ClassName Luidehuaproxy * @Desrition This class to implement Andy Lau proxy class*/ Public classLuidehuaproxy {//designing a class variable to implement the Real object class of this proxy class proxy PrivatePerson person =NewLiudehua (); PublicPerson GetProxy () {//to generate a proxy object for an object return(person) Proxy.newproxyinstance (Luidehuaproxy.class. getClassLoader ()/*loading of proxy classes*/, Person.getclass (). Getinterfaces ()/*class object array, implementing interface object*/, NewInvocationhandler ()/*calling the processor*/ { /*** Invocationhandler interface only defines an invoke method, so for such an interface, we do not have to define a class to implement the interface, * instead of directly using an anonymous inner class to implement the interface, new I Nvocationhandler () {} is an anonymous implementation class for the Invocationhandler interface * In the Invoke method encoding specifies the returned proxy object to do the work * Proxy: Pass the proxy object itself into To * method: passing in the methods of the proxy object's current invocation * args: Pass the method parameter in * * When invoking the proxy object's person.sing ("Ice Rain") or person.dance ("Gangnam Style"); When the method is actually executed is the code inside the Invoke method, so we can use Method.getname () in the Invoke method to know Which method of the proxy object is currently being called*/@Override PublicObject Invoke (Object proxy, Method method, object[] args)throwsThrowable {if(Method.getname (). Equals ("Sing")) { //if the Sing method of the delegate object is calledSystem.out.println ("I am Andy Lau's agent, to find Andy Lau singing needs 1 million!! "); //has paid, the agent himself can not sing only to find Andy Lau sing//the proxy object invokes the Sing method of the real target object to process the user request returnMethod.invoke (person, args); } if(Method.getname (). Equals ("Dance")){ //if the dance method of the delegate object is calledSystem.out.println ("I am Andy Lau's agent, to find Liu Dehua dance needs 1 million!! "); //have paid, the agent himself can not dance only to find Andy Lau dance//the proxy object invokes the dance method of the real target object to process the user request returnMethod.invoke (person, args); } return NULL; } }); }}
4. Testing
Public class Test { publicstaticvoid main (string[] args) { new luidehuaproxy (); = luidehuaproxy.getproxy (); System.out.println (person.sing ("Sea")); System.out.println (Person.dance ("Ah Ah"));} }
Java Agent Concepts