Dynamic agent practice learning notes

xiaoxiao2021-03-06  24

After reading the programmer magazine in January, see the story of the dynamic agent, write down the following learning notes, where Demo has changed, better reflecting the concept of dynamic agents.

I: Dynamic Agent Practice

1. Interface class: foo.java

Package DymaticProxy;

/ **

*

Title: Service interface provided by high-level "

*

description:

*

Copyright: Copyright (C) 2004

*

company: huawei

* @Author zhaolh

* @version 1.0

* /

Public interface foo {

Void doaction ();

} 2. Implementation of the interface 1 package dymaticProxy;

/ **

*

title: Specific implementation of high-level providing service interface

*

description:

*

Copyright: Copyright (C) 2004

*

company:

* @Author zhaolh

* @version 1.0

* /

Public class fooImpl imports foo {

Public fooImpl () {

}

Public void doaction () {

/ ** @ Todo Implement this DymaticProxy.foo Method * /

// throw new java.lang.unsupportedOperationException ("Method doAdition () not yet ustement.");

System.out.println ("in fooimp1.doaction ()");

}

} 3. Implementation of the interface 2 package dymaticproxy;

/ **

*

Title:

*

description:

*

Copyright: Copyright (C) 2004

*

company:

* @Author zhaolh

* @version 1.0

* /

Public class fooImpl2 imports foo {

Public fooImpl2 () {

}

Public void doaction () {

/ ** @ Todo Implement this DymaticProxy.foo Method * /

// throw new java.lang.unsupportedOperationException ("Method doAdition () not yet ustement.");

System.out.println ("in fooimp2.doaction ()");

}

} 4. Processor class package dymaticproxy;

Import java.lang.reflect.method;

Import java.lang.reflect.invocationhandler;

/ **

*

Title: Dynamic Agent Culture Call Processor

*

description:

*

Copyright: Copyright (C) 2004

*

Company: * @author zhaolh

* @version 1.0

* /

Public Class CommonInvocationHandler IMPLEments InvocationHandler {

/ / Dynamic execution object, need to call back

Private Object Target;

/ / Support constructive sub-injection

Public CommonvocationHandler () {

}

/ / Support constructive sub-injection

Public CommonInvocationHandler (Object Target) {

SetTarget;

}

/ **

* Injection with SETTER method

* @Param Target

* /

Public void setTarget (Object Target)

{

THIS.TARGET = Target;

}

/ **

* Call the method specified in Proxy Method, and incorporate a list of parameters args

* @Param Proxy agent type, such as defining a proxy interface corresponding to Method

* @Param Method Method

* @Param Args Parameters to call the proxy method

* @Return

* @Throws java.lang.throwable

* /

Public Object Invoke (Object Proxy, Method Method, Object [] args throws throwable {

/ ** @ Todo Implement this java.lang.reflect.InvocationHandler Method * /

// throw new java.lang.unsupportedOperationException ("Method INVOKE () Not Yet Implement.");

Return Method.Invoke (Target, Args);

}

}

5. Dynamic agent demonstration program package dymaticproxy;

Import java.lang.reflect.invocationhandler;

Import java.lang.reflect.Proxy;

/ **

*

Title:

*

description:

*

Copyright: Copyright (C) 2004

*

company:

* @Author zhaolh

* @version 1.0

* /

Public class demo {

Public static void main (String [] args) {

// 1. General dynamic agent implementation

CommONvocationHandler Handler = New CommonInvocationHandler ();

Foo f;

// 2. Interface implementation 1

Handler.SetTarget (New fooIMPL ());

// Method parameter description: Agency class, proxy class implementation interface list, proxy class processor

// Associated agency class, the interface method in the proxy class, the processor, but when the interface method in the proxy class is called, the INVOKE method for the processor is automatically distributed.

// If the proxy class does not implement the specified interface list, it will throw an illegal parameter exception.

f = (foo) proxy.newproxyinstance (foo.class.getclassloader (),

NEW class [] {foo.class},

Handler);

f.DoAction ();

// 3. Interface implementation 2

Handler.SetTarget (New fooImpl2 ()); f = (foo) proxy.newproxyinstance (foo.class.getclassloader (),

NEW class [] {foo.class},

Handler);

f.DoAction ();

}

} 2: Dynamic agent summary agent class (can be individually interfaces and classes), the proxy class interface (which can be multiple interfaces), the processor constitutes a dynamic relationship, and the creation and running period can be changed. For example, in the above example, the FOO.CLASS.GETCLASSLOADER () instance f can realize the interface in New class [] {foo.class} and associated with the Handler. All methods (new class [] {foo.class}) are forwarded to invoke () () implementation () implementation of all Class [] {foo.class}).

In short, the dynamic agent provides a dynamic proxy class, which can implement any service interface and can determine the interface when running. At the same time, you can implement interception of a unique call method.

转载请注明原文地址:https://www.9cbs.com/read-65526.html

New Post(0)