Java Dynamic Agent

Source: Internet
Author: User

1. Agent mode

From the point of view of design mode, proxy mode is used to decouple two objects, and the broker bridges the Client (caller) and business logic classes as mediations. Typically, a proxy is an existing class that is instantiated and used by the JVM after it is loaded into memory, and can be dynamically generated at run time, making the application of the agent more flexible. The purpose is to provide a proxy for other objects to control access to an object. The proxy class is responsible for preprocessing the message for the delegate class, filtering the message and forwarding the message, and subsequent processing after the message is delegated to the class.

In order to maintain the consistency of the behavior, the proxy class and the delegate class usually implement the same interface, so there is no difference in the visitor's view (transparent). Through the intermediate layer of proxy class, the direct access to delegate class object can be controlled effectively, and the delegate class object can be hidden and protected well, and space is reserved for implementing different control strategies, which gives more flexibility in design. such as proxy objects can encapsulate some internal processing logic (some new features such as access control, logging, caching, authorization, etc.).

The Java dynamic agent mechanism is a clever way to practice the design concept of the proxy mode in near perfect fashion.

2, the traditional proxy mode

Add an additional class proxy between the client class (the caller) and the Business logic class (the callee, the agent), which typically implements the same interface as the agent, and implements the traditional proxy pattern.

As in the proxy class and car both implement the Ivehicle interface:

 Public class Implements ivehicle{    private  ivehicle v;      Public Vehicleproxy (Ivehicle v) {this, v=v;}      Public void start () {    system.out,println ("before Vehicleproxy");    V.start ();    System.out,println ("after Vehicleproxy");    }     // Stop (), Forward (), ...}

3, the principle of dynamic agent

JDK5 introduced a dynamic proxy. Allows developers to create proxy classes and their objects anytime and anywhere at run time, dynamically creating proxy classes that implement multiple interfaces, each associated with an implementation of an Invocationhandler interface that represents internal processing logic.

When a client invokes a method in the proxy object's proxy interface, the invocation information is passed to the Invoke () method of the Invocationhandler interface. The parameters of the Invoke () method can be obtained to the proxy object, the method object corresponding to the methods, and the actual parameters of the call, whose return value is returned to the consumer. )。

This approach is tantamount to intercepting method calls, and AOP is based on the implementation framework of this idea.

  

4, the implementation of dynamic agents

To implement the same functionality using dynamic proxies, first define the class Vehiclehandler that implements the Invocationhandler interface.

When a dynamic proxy is defined, the client's calling method

There seems to be no benefit here, and to write a call processing class (invocation Handler), why use a dynamic proxy? is to produce a generic delegate class (adding functions such as logging) ~

  

5. Resolve public static Object Newproxyinstance in proxy class (ClassLoader loader, class<?>[] interfaces, Invocationhandler h) Method:

/* bind the Vehiclehandler to the Ivehicle interface */ ClassLoader cl=ivehicle. class . getClassLoader (); ivehicle v=(ivehicle) proxy.newproxyinstance (    cl,new class[]{ Ivehicle. class },    new  Vehiclehandler (ivehicle c)    );

This method returns a instance of a proxy class for the specified interfaces, dispatches method invocations to the specified Invocation handler.

(It returns the instance object of the proxy class for the specified interface, this (proxy class?) ) to assign a method call to the specified invocation handler)

Loader, the class loader to define the proxy class.

interfaces, the list of interfaces for the proxy class to implement.

H, the invocation handler to dispatch method invocations to a proxy instance with the specified invocation handler of a P Roxy class that's defined by the specified class loader and that implements the specified interfaces.

6, about the ClassLoader class

The ClassLoader class is used to dynamically load class files into memory.

After we've written a Java program, it's not a CS or BS application, is a complete Java application organized by several. class files, when the program is run, it calls an entry function of the program to invoke the relevant functions of the system, and these functions are encapsulated in different class files, so it is often necessary to call from this class file another class file method, a system exception is thrown if the other file does not exist.

When the program starts, it does not load all the class files used by the program at once, but instead dynamically loads a class file into memory according to the needs of the program through the Java class loading mechanism (ClassLoader), so that only the class file is loaded into memory To be referenced by the other class. So ClassLoader is used to dynamically load class files into memory.

  

Java Dynamic Agent

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.