What is an agent?
What is called a proxy? That is, when calling a method that implements a class, you can do extra work before and after the method is executed, which is the agent.
What about the dynamic agent, the official explanation is:
The emergence of Java dynamic proxy mechanism, so that Java developers do not have to manually write proxy class, as long as a simple set of interfaces and delegate class objects, you can dynamically get the proxy class. The proxy class is responsible for assigning all method calls to reflection execution on the delegate object, and the developer can also adjust the delegate class objects and their functions on demand, which is a very flexible and resilient proxy framework.
I prefer another kind of popular explanation, the official explanation is always highly abstract, it takes a while to understand the experience
Dynamic proxies enable the separation of logs from the business, which means that a class simply provides some business, such as a bank withdrawal business. This class realizes the withdrawal business also needs to implement the logging function, if does not use the dynamic proxy, then this kind of code has already added the log function ability code which oneself should not add additionally. So we have to use the dynamic proxy to separate its business code from the log function code. So using the dynamic agent concept, AOP in spring is a good example.
So, let's take a look at an example of two classes to use
Implement the Java.lang.reflect.InvocationHandler interface to provide an execution processor, that is, the real thing, and then through the java.lang.reflect.Proxy to get a proxy object, through this proxy object to execute the business method, While the business method is invoked, the execution processor is invoked automatically. Remember, dynamic proxies can only be used on interfaces
First Business interface:
Public interface HelloWorld {
public void SayHelloWorld ();
}
And then we're writing this to achieve
public class Helloworldimpl implements HelloWorld {
public void SayHelloWorld () {
System.out.println ("Hello world!");
}
}
Then we thought we could do something else before we could do this, like write a log? See a sister? What, this code does not let change, changed the words, business methods and log mixed mess Ah, later want to change the log format you write AH
Then we have to define an interceptor/execution processor.
public class HelloWorldHandler implements Invocationhandler {
Target Object
Private Object TargetObject;
Public HelloWorldHandler (Object targetobject) {
This.targetobject = TargetObject;
}
public object invoke (object proxy, Method method, object[] args) throws Throwable {
System.out.println ("Before method call");
Object result = Method.invoke (This.targetobject, args);
System.out.println ("Method call End");
return result;
}
}
What's with the client?
public class HelloWorldTest {
public static void Main (string[] args) {
Business Object
HelloWorld obj = new Helloworldimpl ();
Interceptor Object
HelloWorldHandler handler = new HelloWorldHandler (obj);
Returns the proxy object for a business object
HelloWorld proxy = (HelloWorld) proxy.newproxyinstance (
Obj.getclass (). getClassLoader (),
Obj.getclass (). Getinterfaces (),
handler);
Methods of executing business objects through proxy objects
Proxy.sayhelloworld ();
}
}
See, through the Newproxyinstance method of proxy class, incoming class loader, class interface, and this processor, we get a proxy
This is the result of the execution.
Before a method call
Hello world!
Method call End
Well, the computer didn't crash, that's it.