Dynamic agent mechanism of Java

Source: Internet
Author: User

The basic concept and static proxy mechanism of the agent are explained in the previous article: proxy mode of design mode

Now let's talk about the dynamic proxy mechanism of Java

There is an important interface Invocationhandler and an important class proxy in the dynamic agent mechanism of Java, let's look at the official documentation:

Invocationhandler is the interface implemented by the invocation handler of a proxy instance. Each proxy instance have an associated invocation handler. When a method was invoked on a proxy instance, the method invocation was encoded and dispatched to the Invoke method of its Invocation handler.

The invocation handler for the proxy instance needs to implement the Invocationhandler interface.

Each proxy instance is associated with a call handler. When a method is called by the proxy instance, the invocation of the method is encrypted and distributed to the calling method of the caller.

Translation is very awkward, it doesn't matter, the generation will see the code implementation process to understand.

This call handler has a calling method:

Object Invoke (Object proxy, Method method, object[] args) throws Throwable

Processes the calling method of the proxy object and returns the result. This method is called by the calling program when the proxy object binding caller is called.

Proxy object for Proxy called method

Method proxy object calls the appropriate methods of the interface method.

Args passes an array of objects through the proxy object interface call method. The interface method is empty without parameters.

Let's look at the document description of proxy:

public class Proxy extends Object implements Serializable
Proxy provides static methods for creating dynamic proxy classes and instances, and it's also the superclass of all Dynam IC proxy classes created by those methods.

Proxy provides static methods for creating dynamic proxies for classes and objects, and is also a superclass of these methods to create proxy objects.

A dynamic proxy class is a class that implements a sequence of specified interfaces at run time. Proxy methods implement proxy interfaces, proxy classes create proxy objects, Each proxy class is associated with a calling program that implements the Invocationcationhandler interface. The invocation method of the proxy object is distributed to the Invoke method of the instance invocation through his proxy interface. Through the reflection mechanism, the specified method is called, and passes in an array of objects.

    • Some properties of proxy:

    • The proxy class is public,final, and non-abstract

    • The proxy class does not need to specify a valid name, and the class name begins with "$Proxy"

    • Proxy class needs to inherit proxy

    • The proxy class needs to implement each interface specified at the time of creation in order

    • If the proxy class implements a non-public interface, it needs to be defined in the interface under the same name package.

    • Because the proxy class implements all interfaces specified at the time of creation, calling Getinterfaces on the class object will return an array of interface lists, call Getmetjods, An array that implements all the interface methods is returned. Calling GetMethod will return the method expected by proxy.

    • Proxy.isproxyclass Judging is not the proxy method

    • The Java.security.ProtectionDomain of the proxy class, like the system class, is loaded in the startup class

    • Each proxy class has a constructed method that implements the Invocationhandler interface and binds the calling program to the proxy object. Compared with the reflection mechanism to get the construction method, the proxy object can construct the proxy object by calling the Proxy.newproxyinstance method. .

Properties of the Agent interface

    • Proxy class implements Proxy interface

    • Each proxy instance is associated with a calling program by constructing a method. Static method Proxy.getinvocationhandler will return a calling program specified by the proxy object

    • The interface method invocation of the proxy object is encrypted and distributed to the Invoke method of the calling handler.

    • Calls to methods such as hascode,equals,tostring are encrypted and distributed to the Invoke method of the calling program, and the interface methods are encrypted and distributed equally.

Without saying much, the code is on:

First define an interface:

Package com.shadow.proxy.dynamicproxy;/** * * @author Sunny */public interface ITask {public void SetData (String data)    ; public int Getcaldata (int x);}

Define a real topic that implements the interface business:

Package com.shadow.proxy.dynamicproxy;/** * * @author sunny */public class Taskimpl implements ITask {@Override pub    LIC void SetData (String data) {System.out.println (data + "data is saved");    } @Override public int getcaldata (int x) {return x * 10; }    }

The most critical core definition of the proxy class:

package com.shadow.proxy.dynamicproxy;import java.lang.reflect.invocationhandler;import  java.lang.reflect.method;/** * *  @author  sunny */public class dynamicproxy  implements InvocationHandler {    private Object obj;     public dynamicproxy (object obj)  {         this.obj = obj;    }              @Override     public object invoke (object proxy,  Method method, object[] args)  throws Throwable {         object result;        system.out.println (" Start execution method: " + method);         result = method.invoke ( Obj, args);    &nBsp;    system.out.println (method +  "Method execution End");         return result;    }    }

To write a test file:

Package Com.shadow.proxy.dynamicproxy;import Java.lang.reflect.invocationhandler;import java.lang.reflect.Proxy;/ * * * @author sunny */public class Testproxy {public static void main (string[] args) {ITask realtask = new Tas        Kimpl ();        Invocationhandler handler = new Dynamicproxy (realtask); ITask Proxytask = (ITask) proxy.newproxyinstance (Realtask.getclass (). getClassLoader (), Realtask.getclass ().        Getinterfaces (), handler);        Proxytask.setdata ("Come a hamburger");    System.out.println (Proxytask.getcaldata (10)); }}


This article is from "a program of self-cultivation" blog, please be sure to keep this source http://qianqiansun.blog.51cto.com/13271301/1966014

Dynamic agent mechanism of Java

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.