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
- 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, 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.
Results:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Spring2.5 Learning 4.2_ Dynamic Proxy implementation