Analysis and expansion of Java Dynamic proxy mechanism, part 1th

Source: Internet
Author: User

Introduction

The emergence of Java dynamic proxy mechanism, so that Java developers do not have to manually write proxy class, as long as a simple set of interfaces and delegate class objects, you can dynamically get the proxy class. The proxy class is responsible for assigning all method calls to reflection execution on the delegate object, and the developer can also adjust the delegate class objects and their functions on demand, which is a very flexible and resilient proxy framework. By reading this article, readers will have a more in-depth understanding of the Java dynamic proxy mechanism. Starting from the operating mechanism and characteristics of Java dynamic Proxy, this paper analyzes its code and deduce the internal implementation of dynamic generation class.

Agent: Design mode

A proxy is a common design pattern designed to provide a proxy for other objects to control access to an object. The proxy class is responsible for preprocessing messages for the delegate class, filtering messages and forwarding messages, and performing subsequent processing of the message after the class is executed.

Figure 1. Agent mode

In order to maintain the consistency of behavior, the proxy class and the delegate class usually implement the same interface, so there is no difference between the two in the visitor's view. Through the middle layer of the proxy class, it can effectively control the direct access to the entrusted class object, and also can hide and protect the object of the delegate class, and also reserve space for implementing different control strategies, thus obtaining more flexibility in the design. The Java dynamic proxy mechanism has practically perfected the design concept of Agent mode in a clever way.

Related classes and interfaces

To understand the mechanism of Java dynamic proxies, you first need to understand the following related classes or interfaces:

Java.lang.reflect.Proxy: This is the main class of the Java dynamic proxy mechanism, which provides a set of static methods for dynamically generating proxy classes and their objects for a set of interfaces.

Listing 1. static method of Proxy

// 方法 1: 该方法用于获取指定代理对象所关联的调用处理器
static InvocationHandler getInvocationHandler(Object proxy)

// 方法 2:该方法用于获取关联于指定类装载器和一组接口的动态代理类 的类对象
static Class getProxyClass(ClassLoader loader, Class[]  interfaces)

// 方法 3:该方法用于判断指定类对象是否是一个动态代理类
static boolean isProxyClass(Class cl)

// 方法 4:该方法用于为指定类装载器、一组接口及调用处理器生成动态 代理类实例
static Object newProxyInstance(ClassLoader loader, Class[]  interfaces,
   InvocationHandler h)

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.