Java Dynamic Agent One--the use of dynamic class proxy
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
1PackageCom.ljq.test;
2
3/**
4* Define a HelloWorld interface
5*
6 * @author Jiqinlin
*
8 */
9 public Interface HelloWorld {
10 public void SayHelloWorld ();
11 "
<br/>
Class Helloworldimpl is the implementation of the HelloWorld interface
1PackageCom.ljq.test;
2
3/**
4* Class Helloworldimpl is the implementation of the HelloWorld interface
5*
6*@authorJiqinlin
7*
8 */
9Public Class Helloworldimpl implements helloworld{
10 11 public void SayHelloWorld () {
12 System.out.println ( "helloworld!" );
13 14
15
HelloWorldHandler is a Invocationhandler interface implementation
1PackageCom.ljq.test;
2
3ImportJava.lang.reflect.InvocationHandler;
4ImportJava.lang.reflect.Method;
5
6/**
7* Implement output of two strings to the console before and after a method call
8*
9*@authorJiqinlin
10*
11 */
12Public ClassHelloWorldHandlerImplementsinvocationhandler{
13 //The original object to be proxied
14 PrivateObject obj;
15
16 PublicHelloWorldHandler (Object obj) {
17 Super();
18 This. obj=Obj
19}
20
21st /**
22* Process the method call on the proxy instance and return the result
23*
24*@paramproxy class
25*@parammethod is represented by methods
26*@paramArgs An array of arguments to the method
27 */
28 PublicObject Invoke (Object proxy, Method method, object[] args)ThrowsThrowable {
29Object result= Null;
30 //Before calling
31Dobefore ();
32 //Methods to invoke the original object
33Result=Method.invoke (obj, args);
34 //After the call
35Doafter ();
36 ReturnResult
37}
38
39 Private voidDobefore () {
40System.out.println ("Before method invoke");
41 42
43 private void Doafter () {
System.out.println (" );
45
46
47
Test class
Package com.ljq.test;
Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Proxy;
Public ClassHelloWorldTest {
Public static void Main (string[] args) {
HelloWorld helloworld=< Span style= "color: #0000ff;" >new Helloworldimpl ();
Invocationhandler handler=new HelloWorldHandler (HelloWorld);
// Create dynamic proxy object
HelloWorld proxy= ( HelloWorld) proxy.newproxyinstance (
Helloworld.getclass (). getClassLoader (),
Helloworld.getclass (). Getinterfaces (),
handler);
Proxy.sayhelloworld ();
}
} /span>
Java Dynamic Proxy