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
<br/>
Define a HelloWorld interface
1 Packagecom.ljq.test;
2
3 /**
4 * Define a HelloWorld interface
5 *
6 * @authorJiqinlin
7 *
8 */
9 PublicInterfaceHelloWorld {
Ten PublicvoidSayHelloWorld ();
One }
<br/>
Class Helloworldimpl is the implementation of the HelloWorld interface
1 Packagecom.ljq.test;
2
3 /**
4 * Class Helloworldimpl is the implementation of the HelloWorld interface
5 *
6 * @authorJiqinlin
7 *
8 */
9 PublicclassHelloworldimplImplementshelloworld{
Ten
One PublicvoidSayHelloWorld () {
A System.out.println ("helloworld!");
- }
-
the }
HelloWorldHandler is a Invocationhandler interface implementation
1 Packagecom.ljq.test;
2
3 ImportJava.lang.reflect.InvocationHandler;
4 ImportJava.lang.reflect.Method;
5
6 /**
7 * Implement output of two strings to the console before and after a method call
8 *
9 * @authorJiqinlin
Ten *
One */
A PublicclassHelloWorldHandlerImplementsinvocationhandler{
- //the original object to be proxied
- PrivateObject obj;
the
- PublicHelloWorldHandler (Object obj) {
- Super();
- This. obj=obj;
+ }
-
+ /**
A * Process the method call on the proxy instance and return the result
at *
- * @paramproxy class
- * @parammethod is represented by methods
- * @paramargs An array of arguments to the method
- */
- Publicobject Invoke (Object proxy, Method method, object[] args)throwsThrowable {
in Object Result=NULL;
- //before calling
to Dobefore ();
+ //methods to invoke the original object
- result=method.invoke (obj, args);
the //after the call
* Doafter ();
$ returnResult
Panax Notoginseng }
-
the PrivatevoidDobefore () {
+ System.out.println ("before method Invoke");
A }
the
+ PrivatevoidDoafter () {
- System.out.println ("After method Invoke");
$ }
$
- }
Test class
Package Com.ljq.test;
Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Proxy;
PublicclassHelloWorldTest {
PublicStaticvoidMain (string[] args) {
HelloWorld HelloWorld=NewHelloworldimpl ();
Invocationhandler Handler=NewHelloWorldHandler (HelloWorld);
//To create a dynamic proxy object
HelloWorld Proxy=(HelloWorld) proxy.newproxyinstance (
Helloworld.getclass (). getClassLoader (),
Helloworld.getclass (). Getinterfaces (),
handler);
Proxy.sayhelloworld ();
}
}
The result of the operation is:
43, Java dynamic Agent one--the use of dynamic class proxy