1.1. Java Dynamic proxy
Proxy (create proxy object) and invocationhandler (call forwarding) under java. Lang. Reflect package
The proxy object represents the target object to execute the corresponding activity.
Static Proxy: the proxy object implements the same interface of the target object, implements proxy interface and call forwarding (not recommended)
The additional logic before and after the call reflects the value of the proxy object
Sample Code:
1. Java Dynamic proxy interface package proxy; public interface bookdao {// Add public void addbook ();} 2. Create an implementation class (target class) package proxy; public class bookdaoimpl implements bookdao {@ overridepublic void addbook () {// todo auto-generated method stubsystem. out. println ("add the implementation method of books .... ") ;}} 3. Create a proxy package proxy; import Java. lang. reflect. invocationhandler; import Java. lang. reflect. method; import Java. lang. reflect. proxy; // JDK dynamic proxy class implements public class bookdaoproxy implements invocationhandler {// actual proxy object private object target; // bind the delegate object and return a proxy class public object BIND (Object targetdomainthis.tar get = target; // get the proxy object // interface to be bound (this is a defect, cglib makes up for this defect) return proxy. newproxyinstance (this. getclass (). getclassloader (), this.tar get. getclass (). getinterfaces (), this) ;}@ overridepublic object invoke (Object proxy, method, object [] ARGs) throws throwable {// todo auto-generated method stubobject result = NULL; system. out. println ("START transaction processing... "); // execution method result = method. invoke (target, argS); system. out. println ("transaction end processing... "); return result;} 4. Test class: Package proxy; public class booktest {public static void main (string [] ARGs) {// obtain a dynamic proxy class bookdao proxy = (bookdao) New bookdaoproxy (). BIND (New bookdaoimpl (); // call the proxy method proxy. addbook ();}}
1.1. Java NiO (self-Research)
Network Programming