Java: static proxy and dynamic proxy, java static and dynamic

Source: Internet
Author: User

Java: static proxy and dynamic proxy, java static and dynamic

The proxy mode is a common design mode. It features that the proxy class has the same interface as the delegate class. In specific implementation, there are static proxy and dynamic proxy. There is usually an association between the proxy class and the delegate class. A proxy class object is associated with a delegate class object. The proxy class object itself does not actually implement the service, but calls the relevant methods of the delegate class object, to provide specific services, that is, the proxy class is mainly responsible for preprocessing, filtering, forwarding, and post-processing messages for the delegate class.

There is a significant difference between static proxy and dynamic Proxy:

Static Proxy: the source code is automatically generated by the programmer or a specific tool, and then compiled. Before running the program, the. class file of the proxy class already exists.
Dynamic Proxy: the bytecode of the dynamic proxy classJava reflection mechanismDynamic generation, without the need for programmers to manually write its source code.

 

Static Proxy:

 

Dynamic Proxy:

 

JDK dynamic proxy explanation:

The InvocationHandler interface and Proxy class in the java. lang. reflect package provide the ability to generate dynamic Proxy classes.

(1) InvocationHandler interface:

public interface InvocationHandler {     public Object invoke(Object proxy,Method method,Object[] args) throws Throwable; }

Parameter description:
Object proxy: the proxy Object.
Method method: Method to be called
Object [] args: parameters required for method calling

The subclass of the InvocationHandler interface can be considered as the final operation class of a proxy, as defined in the above example.

 

(2) Proxy class:

The Proxy class is an operation class dedicated to Proxy. You can use this class to dynamically generate an implementation class for one or more interfaces. This class provides the following operation methods:

public static Object newProxyInstance(ClassLoader loader,                                          Class<?>[] interfaces,                                          InvocationHandler h)        throws IllegalArgumentException    {        if (h == null) {            throw new NullPointerException();        }        /*         * Look up or generate the designated proxy class.         */        Class<?> cl = getProxyClass(loader, interfaces);        /*         * Invoke its constructor with the designated invocation handler.         */        try {            Constructor cons = cl.getConstructor(constructorParams);            return cons.newInstance(new Object[] { h });        } catch (NoSuchMethodException e) {            throw new InternalError(e.toString());        } catch (IllegalAccessException e) {            throw new InternalError(e.toString());        } catch (InstantiationException e) {            throw new InternalError(e.toString());        } catch (InvocationTargetException e) {            throw new InternalError(e.toString());        }    }

Parameter description:
ClassLoader loader: Class loader
Class <?> [] Interfaces: Get all interfaces
InvocationHandler h: obtains the subclass instance of the InvocationHandler interface.

 

See the author's analysis of the above source code in the following article.

[Original] JDK dynamic proxy, which will never be forgotten after this time. Http://www.cnblogs.com/dreamroute/p/5273888.html

A thorough understanding of JAVA Dynamic proxy http://www.cnblogs.com/flyoung2008/p/3251148.html

 

Extension:

JDK dynamic proxy relies on interfaces. If some classes do not implement interfaces, JDK proxy cannot be used.

Spring AOP has two methods to implement dynamic proxy. If Programming Based on interfaces, JDK dynamic proxy is used by default; otherwise, cglib is used, in addition, you can use cglib in the spring configuration file for dynamic proxy. The principle of cglib is to generate a subclass for the specified target class and overwrite the method to implement enhancement. However, because the class is inherited, the final modified class cannot be used as a proxy.

Note: You need to check the cglib implementation method in this section.

 

Related Article

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.