Spring2.5 Learning 4.2_proxy Implement dynamic Proxy (target class implements arbitrary interface)

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 agent is not, through the dynamic proxy can be any implementation of a certain interface of the class to enhance the functionality.

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

Hellointerface interface

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

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, which avoids the fixed defect of the static interface, and the dynamic agent is shifted from the original static agent oriented interface to the dynamic agent oriented implementation class, so the starting point of the design is not the interface. But the dynamic interface, the flexibility of software is greatly improved.

When you call the method of an instance of the Hellointerfaceimpl class that is being proxied, the system runs the corresponding code in the Invoke method that includes the Invocationhandler of the proxy class protest. The process of the dynamic agent 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 feels 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.



Results:

Spring2.5 Learning 4.2_proxy Implement dynamic Proxy (target class implements arbitrary interface)

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.