Dynamic Agent
1. Learn only one method:
Object proxyobject = proxy.newproxyinstance (ClassLoader ClassLoader, class[] interfaces, Invocationhandler h);
The function of the method: Dynamically creates a set of implementation class objects for the specified interface at run time! (At run time, create an object that implements a specified set of interfaces)
Interface A {
}
Interface B {
}
Object o = Method (New Class[]{a.class,b.class})
O It implements A and B two interfaces!
?
Object proxyobject = proxy.newproxyinstance (ClassLoader ClassLoader, class[] interfaces, Invocationhandler h);
1. Method function: Dynamic creation implements the implementation class object of all specified interfaces in the interfaces array!
Three major parameters are introduced:
1. ClassLoader: Class Loader!
* It is used for loaders, loading. class files into memory, forming class objects!
2. class[] Interfaces: Specify the interfaces you want to implement
3. Invocationhandler: All methods of the Proxied object (not executed individually, getclass ()) Call the Invocationhandler Invoke () method.
Description: In other words, implementing a dynamic proxy requires three parameters.
2. Dynamic Agent Action
Finally, learning AOP (aspect-oriented programming), which is a bit like decorator mode, is more flexible than decorator mode!
Invocationhandler
public object invoke (object proxy, Method method, object[] args);
?
When this invoke () method is called!
1. When the proxy object is created? Wrong!
2. When invoking a method in an interface implemented by a proxy object? Right!
?
* Object Proxy: The current object, that is, the object being proxied! In calling who's Method!
* Method: Methods currently being called (method of the target object)
* object[] args: Arguments!
The correspondence between them:
Target object: The object being enhanced
Proxy object: A target object is required and an enhanced object is added to the target object!
Target method: Enhanced Content
Proxy object = target object + Enhanced
?
Example: use three parameters to create a proxy object: Dome1.java
/** * @function : use three parameters to create a proxy object * @author not-bug * */ Public class Demo1 { ???? @Test ???? Public void fun1 () { ???????? /* ???????? * three major parameters ???????? * 1. ClassLoader ???????? * method requires a dynamic generation of a class that implements the A , B interface, and then create an object of this class! ???????? * need to generate a class, this class also needs to be loaded into the method area, who will load, of course , ClassLoader !! ???????? * ???????? * 2. Class[] Interfaces ???????? * It is the interface that you want to implement, that is, the interface you need to implement ???????? * ???????? * 3. Invocationhandler ???????? * It is called the processor ???????? * Perfunctory it! ???????? * ???????? * The method in the proxy object implements all interfaces, and the content is called Invocationhandler of the Invoke () method. ???????? */ ???????? ClassLoader Loader = This . GetClass (). getClassLoader (); ???????? Invocationhandler h = new Invocationhandler () { ???????????? Public object Invoke (Object proxy, method method, object[] args) ???????????????????? throws throwable { ???????????????? System. out. println (" Hello, dynamic agent! "); ???????????????? return"xxx"; ????????????} ????????}; ???????? // Create a proxy object with three parameters!!! ???????? Object o = Proxy. Newproxyinstance(loader, new class[]{aClass, B. class }, h); ???????? ???????? // Strong turn into a and B type, Success! ???????? A = (a) o; ???????? b b = (b) o; ???????? //???????? A.A (); //???????? A.aa (); //???????? B.B (); //???????? B.BB (); ???????? //???????? System.out.println (O.getclass (). GetName ()); ???????? // invoking an interface method implemented by a proxy object is actually called the Invoke () method of the invocationhandler . ???????? Object Result = a. AAA ("Hello", +); ???????? System. out. println (result); ????} } ? Interface A { ???? Public void A (); ???? Public void aa (); ???? Public Object aaa (String s, inti); } ? Interface B { ???? Public void B (); ???? Public void BB (); } |
?
This instance runs the result:
Dynamic Proxy 1