1. interface of the proxy object:
Package test. dynamicproxy; public interface targetinterface {public void sayhello (); Public int sum (int A, int B );}
2. Proxy objects:
Package test. dynamicproxy; public class target implements targetinterface {public void sayhello () {system. out. println ("hello");} public int sum (int A, int B) {return a + B ;}}
3. invocationhandler packaging:
Package test. dynamicproxy; import Java. lang. reflect. invocationhandler; import Java. lang. reflect. method; public class targetinvocationhandler implements invocationhandler {private object; Public targetinvocationhandler (Object OBJ) {This. object = OBJ;} public object invoke (Object proxy, method, object [] args2) throws throwable {dobefore (); object result = method. invoke (object, args2); doafter (); return result;} public void dobefore () {system. out. println ("do before");} public void doafter () {system. out. println ("do after ");}}
4. Test class:
package test. dynamicproxy; import Java. lang. reflect. proxy; public class testdynamicproxy {/*** @ Param ARGs */public static void main (string [] ARGs) {target T = new target (); targetinvocationhandler handler = new targetinvocationhandler (t); targetinterface proxy = (targetinterface) proxy. newproxyinstance (T. getclass (). getclassloader (), T. getclass (). getinterfaces (), Handler); proxy. sayhello (); int B = proxy. sum (10, 20); system. out. println (B) ;}}