Java Dynamic Agent

Source: Internet
Author: User

Proxy, that is, the proxy class and the proxy class have the same external interface or service, so the proxy class generally must implement all the proxy class has been implemented interface, because the interface is to develop a series of external services standards.

Because dynamic proxies have such a flexible nature, we do not have to explicitly implement the same interface (interface) as the real topic class (Realsubject) When designing the Dynamic proxy class (Dynamicproxy), but defer this implementation to runtime.

In order to enable the Dynamicproxy class to implement a series of interfaces implemented by the Realsubject class at runtime, and to perform the related method operations in the interface, The Dynamicproxy class needs to implement the Java.lang.reflect.InvocationHandler interface that comes with the JDK, and the Invoke () method in the interface allows the Dynamicproxy instance to invoke the "external service" of the proxy class at run time. , that is, calling the methods in all interfaces that the proxy class needs to implement externally, that is, to complete the invocation of the real method, which is called the real method in the Java Help document as the handler.

We must first load all the interface that have been implemented by the proxy class realsubject into the JVM, otherwise how can the JVM find these methods? Understand this, then we can create an instance of the proxy class to get the class loader ClassLoader for that instance.

The so-called ClassLoader, ClassLoader, is a class definition with a class, that is, the internal related structure of the class (including the inheritance tree, the method area, and so on)

Dynamic proxy mode allows us to extend and enhance the original "real method" without changing the existing code structure, and can achieve the purpose of controlling the behavior of the Proxied object. The Invoke () method that the Dynamicproxy class must implement can perform certain special operations before and after invoking the real method of the proxy class. This is the most obvious advantage of dynamic agents


In order to solve some problems, for example, direct access to certain classes is not allowed, and special processing is done for access. Or, to have a unified extension of the original method, there are dynamic proxy requirements

Class diagram


The JDK dynamic agent contains a class and an interface:
Invocationhandler Interface:



Parameter description:
Object proxy: Refers to the objects being proxied.
Method: Methods to invoke
object[] args: Parameters required for method invocation

The subclass of the Invocationhandler interface can be imagined as the final action class of an agent

All methods declared in the interface are transferred to a centralized method for processing (Invoke)

The dynamic proxy class can only proxy interfaces, and the proxy class needs to implement the Invocationhandler class to implement the Invoke method. The Invoke method is called when calling all methods of the Proxied interface, and the value returned by the Invoke method is an implementation class of the Proxy interface

Topic interface

public interface Isubject {void dosomething ();}

Real Objects

public class Realsubject implements Isubject {@Overridepublic void dosomething () {System.out.println ("+++realsubject++ +");}}

Handler

public class Subhandler implements Invocationhandler {Private object obj;//holds real object public subhandler (object obj) {this.obj = obj;} @Overridepublic object Invoke (Object proxy, Method method, object[] args) throws Throwable {Object result = Method.invoke (o BJ, args);//real object to execute specific method System.out.println ("Dy proxy");//Add own operation return result;}}

Use

public class Test {public static void main (string[] args) {Realsubject realsubject = new Realsubject (); Isubject sub = (ISu bject) Proxy.newproxyinstance (Realsubject.getclass (). getClassLoader (), Realsubject.getclass (). GetInterfaces (), New Subhandler (Realsubject)); sub.dosomething ();}}

Output

+++realsubject+++

DY Proxy






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.