I. What is an agent (mediation) target object/proxied object------Homeowner: The real way to rent a house
Proxy object-------Black intermediary: A way to rent a house (call the landlord's rental method)
Object that executes the proxy object method----renting people
Process: We want to rent-----> Intermediary (Rental method)------> Homeowner (Rental method)
Abstraction: Calling Object-----> Proxy object------> Target Object
Dynamic Agent Dynamic Agent: Do not have to manually write a proxy object, do not need one by one to write the same method as the target object, this process, in the runtime's memory dynamically generated proxy objects. A proxy object at the------byte-code object level
API for Dynamic Agents:
There is a method in the JDK API that generates a dynamic proxy in a proxy newproxyinstance
Case one:
Target
Package com.ken.proxy;
public class Target implements Targetinterface {
@Override public
void Method1 () {
System.out.println (" Method1 running ... ");
@Override public
String method2 () {
System.out.println ("Method2 running ...");
return "METHOD2";
}
}
Targetinterface
Package com.ken.proxy;
Public interface Targetinterface {public
void method1 ();
Public String method2 ();
}
Proxytest
Package com.ken.proxy;
Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Proxy;
Import Org.junit.Test;
public class Proxytest {@Test public void test1 () {Invocationhandler h; Obtain a dynamic proxy object----Dynamically create a virtual proxy object for the target in memory at run time//Objproxy is the proxy object based on the parameters to determine who exactly is the proxy object targetinterface objproxy = (targetinte Rface) Proxy.newproxyinstance (Target.class.getClassLoader (),//the same class loader as the target object new class[] {Targetinterface.class} , new Invocationhandler () {///Invoke: Represents a method that executes a proxy object @Override//method: A way to represent the target object bytecode object//args: Represents the target object's The parameters of the corresponding method public object invoke (object proxy, Method method, object[] args) throws Throwable {System.out.println
("Logic before the target method");
Method of executing the target object, object invoke = Method.invoke (new target (), args);
System.out.println ("Logic after the target method");
return invoke;
}
});
Objproxy.method1 ();
String method2 = Objproxy.method2 ();
System.out.println (METHOD2); }
}
Case TWO:
Target
public class Target implements targetinterface{
@Override public
void Method1 () {
System.out.println (" Method1 running ... ");
@Override public
String method2 () {
System.out.println ("Method2 running ...");
return "METHOD2";
}
@Override public
int method3 (int x) {
return x;
}
}
Targetinterface
Public interface Targetinterface {public
void method1 ();
Public String method2 ();
public int method3 (int x);
}
ProxyTest2
public class ProxyTest2 {
public static void main (String [] args) {
final Target target = new Target ();
// Dynamic creation of proxy objects
TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance (
target.getClass (). getClassLoader (),
target.getClass (). getInterfaces (),
new InvocationHandler () {
@Override
// is executed several times. ------- See how many times the proxy object calls the method
// The proxy object calls the corresponding method of the interface.
/ *
* proxy: is the proxy object
* method: represents the bytecode object of the target method
* args: represents the parameter when calling the target method
* /
public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {
// Reflect knowledge
Object invoke = method.invoke (target, args); // The corresponding method of the target object
// retrun returns the value to the proxy object
return invoke;
}
}
);
proxy.method1 (); // Invoke invoke --- Method: method1 method of the target object args: null return value null
String method2 = proxy.method2 (); // Invoke invoke --- Method: method2 method of the target object args: null return method2
int method3 = proxy.method3 (100); //// Invoke ----- Method: method3 method of the target object args: Object [] {100} returns a value of 100
System.out.println (method2);
System.out.println (method3);
}
}
Note: The JDK proxy way to implement the dynamic proxy, the target object must have an interface, no interface can not implement the JDK version of the dynamic agent.