Spring2.5 Learning 4.2_ Dynamic Proxy implementation

Source: Internet
Author: User

The disadvantage of a static proxy is that it binds a fixed interface in the proxy class, is not conducive to the extension, the dynamic proxy is not, the dynamic agent can be any implementation of the class of an interface to enhance the functionality.

in Java, dynamic proxies are created by the Invocationhander to achieve.

Hellointerface interface

[Java]View Plaincopy
  1. Package  proxy;
  3. public   interface  hellointerface {   
  4.     public   void  sayhello ();  
  5. }
Implementclass Hellointerfaceimpl for Hellointerface interfaces
View Plaincopy
  1. Package  proxy;
  2. public   class  hellointerfaceimpl  implements  hellointerface {   
  3. @Override   
  4.     public   void  sayhello ()  {   
  5.         system.out.println (" HELLO XIANJJ "
  6. }
  7. }

The protest class implements the Invocationhandler class

Package Proxy;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;import java.lang.reflect.proxy;//implement Invocationhandler interface public class protest implements Invocationhandler {Object anyobject;/ /Assert Bind method public object bind (object anyobject) {this.anyobject = Anyobject;return proxy.newproxyinstance ( Anyobject.getclass (). getClassLoader (), Anyobject.getclass (). Getinterfaces (), this);}  Override the Invoke method @overridepublic object Invoke (Object arg0, Method arg1, object[] args) throws Throwable {Object returnobject = Null SYSTEM.OUT.PRINTLN ("Dynamic agent Start"); Returnobject = Arg1.invoke (Anyobject, args); SYSTEM.OUT.PRINTLN ("Dynamic proxy End"); return returnobject;} public static void Main (string[] args) {Protest protest = new protest (); Hellointerface Hellointerfacereturn = (hellointerface) protest.bind (New Hellointerfaceimpl ()); Hellointerfacereturn.sayhello ();}}
The bind binding method that calls the protest object in the main method binds an implementation class to a dynamic proxy, and the dynamic proxy is implemented primarily by the following code

Proxy.newproxyinstance (Anyobject.getclass (). getClassLoader (), Anyobject.getclass (). Getinterfaces (), this);
The input Anyobject object is dynamically associated with the interface, so that the static interface fixed defects can be avoided, the dynamic agent from the original static agent oriented interface to the dynamic agent oriented implementation class, so that the starting point of the design is not the interface, but the dynamic fetch interface, software flexibility greatly improved.

When you call the method of an instance of the Hellointerfaceimpl class that is being proxied, the system goes to the Invoke method that contains the proxy class protest invocationhandler to execute the corresponding code, and the dynamic agent process ends.

The public class protest implements Invocationhandler class does not serve an interface, both static and dynamic proxies are enhanced for the SayHello () method. Field field level enhancements are not supported. Spring thinks that it is already destroying the structure of object-oriented programming, so the enhancement of the support method is no longer appropriate, and the integration with other spring modules will be more centralized.


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

Spring2.5 Learning 4.2_ Dynamic Proxy implementation

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.