Java Dynamic Agent One--the use of dynamic class proxy
For a more detailed reference: http://blog.csdn.net/lovelion/article/details/8116704
1. What is a dynamic agent?
A: Dynamic agents can provide access to another object while hiding the exact facts of the actual object. The agent generally implements the interface of the actual object it represents. The agent can access the actual object, but delay the realization of some functions of the actual object, the actual object realizes the actual function of the system, and the proxy object hides the actual object from the customer. The customer does not know whether it deals with the agent or with the actual object.
2. Why use dynamic agents?
A: Because the dynamic agent can do any processing of the request
3. What are the benefits of using it?
A: Because the dynamic agent can do any processing of the request
4. Where do I need dynamic proxies?
A: Do not allow direct access to certain classes, special handling for access, etc.
Support for dynamic proxies is currently included in the Java development package, but its implementation supports only the implementation of interfaces . Its implementation is mainly through the Java.lang.reflect.Proxy class and the Java.lang.reflect.InvocationHandler interface.
The proxy class is primarily used to obtain dynamic proxy objects, which are used by the Invocationhandler interface to constrain the caller implementation
The following is a simulation case where a two-sentence string is output to the console before and after a method call through a dynamic proxy implementation
Directory structure
Define a HelloWorld interface
Package/** * Defines a HelloWorld interface * @author */ publicInterface HelloWorld { publicvoid SayHelloWorld ();}
Class Helloworldimpl is the implementation of the HelloWorld interface
package/** * Class Helloworldimpl is the implementation of the HelloWorld interface *@author * /publicclassimplements helloworld{ publicvoid SayHelloWorld () { System.out.println ("helloworld!") ); }}
HelloWorldHandler is a Invocationhandler interface implementation
Packagecom.ljq.test;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;/*** Implement output of two strings to the console before and after a method call *@authorJiqinlin **/ Public classHelloWorldHandlerImplementsinvocationhandler{//the original object to be proxied PrivateObject obj; PublicHelloWorldHandler (Object obj) {Super(); This. obj =obj; } /*** Process The method call on the proxy instance and return the result * *@paramproxy class *@parammethod being proxied by *@paramargs An array of arguments to the method*/ PublicObject Invoke (Object proxy, Method method, object[] args)throwsthrowable {Object result=NULL; //before callingDobefore (); //methods to invoke the original objectresult=method.invoke (obj, args); //after the callDoafter (); returnresult; } Private voidDobefore () {System.out.println ("Before method Invoke"); } Private voidDoafter () {System.out.println ("After method Invoke"); } }
Test class
Packagecom.ljq.test;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Proxy; Public classHelloWorldTest { Public Static voidMain (string[] args) {HelloWorld HelloWorld=NewHelloworldimpl (); Invocationhandler Handler=NewHelloWorldHandler (HelloWorld); //To create a dynamic proxy objectHelloWorld proxy=(HelloWorld) proxy.newproxyinstance (Helloworld.getclass (). getClassLoader (), HelloWorld. GetClass (). Getinterfaces (), handler); Proxy.sayhelloworld (); }}
The result of the operation is:
JAVA SE Basics Review-class and Reflection (2)