Java: Static proxy and dynamic proxy

Source: Internet
Author: User

Proxy mode is a common design pattern, and its characteristic is that the proxy class and the delegate class have the same interface, in the concrete implementation, there are static agents and dynamic agents. There is usually an association between the proxy class and the delegate class, and the object of a proxy class is associated with an object of a delegate class, and the object of the proxy class does not actually implement the service, but instead provides a specific service by invoking the method of the object of the delegate class, that is, the proxy class is primarily responsible for preprocessing the message, filtering the Forwards the message to the delegate class, as well as the post-processing message.

A significant difference between static proxies and dynamic proxies:

Static proxy: The source code is generated automatically by the programmer or a specific tool, and then compiled. Before the program runs, the. class file for the proxy classes already exists.
Dynamic Proxy: The bytecode of the dynamic proxy class is dynamically generated by the Java Reflection mechanism when the program runs, without the programmer writing its source code manually.

Static Proxy:

Dynamic Agent:

JDK Dynamic Agent Commentary:

The Invocationhandler interface and the proxy class in the Java.lang.reflect package provide the ability to generate dynamic proxy classes.

(1) Invocationhandler interface:

 Public Interface Invocationhandler {     publicthrows  throwable;}

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

You can think of a subclass of the Invocationhandler interface as the final action class of an agent, as defined in the previous example of the Loghandler class.

(2) proxy class:

The proxy class is an action class that specifically completes the proxy, which can be used to dynamically generate an implementation class for one or more interfaces, which provides the following operations:

 Public StaticObject newproxyinstance (ClassLoader loader, Class<?>[] interfaces, Invocationhandler h)throwsIllegalArgumentException {if(H = =NULL) {            Throw NewNullPointerException (); }        /** 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); returnCons.newinstance (Newobject[] {h}); } Catch(nosuchmethodexception e) {Throw NewInternalerror (e.tostring ()); } Catch(illegalaccessexception e) {Throw NewInternalerror (e.tostring ()); } Catch(instantiationexception e) {Throw NewInternalerror (e.tostring ()); } Catch(InvocationTargetException e) {Throw NewInternalerror (e.tostring ()); }    }

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

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

"Original" JDK dynamic agent, this time, forever unforgettable. Http://www.cnblogs.com/dreamroute/p/5273888.html

Thoroughly understand the Java dynamic Agent http://www.cnblogs.com/flyoung2008/p/3251148.html

Extended:

The dynamic proxy of the JDK relies on an interface, and if some classes do not implement an interface, the JDK proxy cannot be used.

Spring AOP has two ways to implement dynamic Proxy, if based on interface programming, the default is the JDK dynamic agent, otherwise is the Cglib way, in addition to spring configuration file can also be set to use Cglib to do dynamic proxy. The principle of cglib is to generate a subclass of the specified target class and override the method implementation enhancements, but because inheritance is used, the final decorated class cannot be proxied.

Note: This section needs to see how the Cglib is implemented

Java: Static proxy and dynamic proxy

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.