implementation Process
1, proxy object
2, interface
3, target object source code proxy Object
Package com.test;
Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Proxy;
public class Main {public
static void Main (string[] args) {
//target object (delegate object)
final Userserviceimpl target = new Userserviceimpl ();
UserService interface
UserService service = (userservice) proxy.newproxyinstance (Main.class.getClassLoader (), New Class[]{userservice.class}, new Invocationhandler () {Public
object Invoke (Object Proxy, Method method, object[] args) throws Throwable {
System.out.println ("Before Execution");
Object result = Method.invoke (target, args);
System.out.println ("after Execution");
return result;
}
);
System.out.println (Service.getage ());
}
UserService interface
Package com.test;
Public interface UserService {
Boolean isadult ();
Integer getage ();
}
Userserviceimpl interface Implementation Class
Package com.test;
public class Userserviceimpl implements UserService {public
Boolean Isadult () {return
false;
}
Public Integer Getage () {return
}
}