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
- Package proxy;
-   
- public   interface  HELLOINTERFACE {  
- public   void sayhello ();
- }
Implementclass Hellointerfaceimpl for Hellointerface interfaces
[Java]View Plaincopy
- package  PROXY;  
-   
- public   class hellointerfaceimpl implements  HELLOINTERFACE {  
-   
- @Override   
- public   void sayhello () {
- system.out.println (" HELLO XIANJJ "
- }
- }
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)