Package COM. CZP. util; import Java. lang. reflect. invocationhandler; import Java. lang. reflect. method; import Java. lang. reflect. proxy;/*** the proxy generation factory requires its subclass to implement before (...), after (...), onexception (...) implement business processing ** @ author caozhongping **/public abstract class proxyfacty <t> implements invocationhandler {private t target; Public proxyfacty (T target) implements super(%%%this.tar get = target ;} /*** create a proxy. If the target class does not implement the interface, return the self ** @ par Am target * @ return */@ suppresswarnings ("unchecked") Public t getproxy () {class <t> CLS = (class <t>) target. getclass (); If (CLS. getinterfaces ()! = NULL) {return (t) proxy. newproxyinstance (CLS. getclassloader (), CLS. getinterfaces (), this);} return target;}/*** call through proxy */@ overridepublic object invoke (Object proxy, method, object [] ARGs) throws throwable {object OBJ = NULL; try {before (ARGs); OBJ = method. invoke (target, argS); after (ARGs);} catch (exception e) {onexception (E);} return OBJ ;} /*** handle exceptions when exceptions occur ** @ Param ex */public abstract void onexception (exception ex ); /*** call method post-processing ** @ Param ARGs */public abstract void after (object [] ARGs ); /*** call Method Pre-processing ** @ Param ARGs */public abstract void before (object [] ARGs);} // the target object interface package COM. CZP. util; public interface testitf {void say (string name) throws exception;} // target object package COM. CZP. util; public class testitfimpl implements testitf {@ overridepublic void say (string name) throws exception {If (name = NULL) throw new exception ("name is null"); system. out. println ("hell," + name) ;}// the proxy tool class inherits the factory class to intercept package COM. CZP. util; public class proxyutil <t> extends proxyfacty <t> {public proxyutil (T target) {super (target) ;}@ overridepublic void onexception (exception ex) {system. out. println ("Call method error:" + ex) ;}@ overridepublic void after (object [] ARGs) {system. out. println ("---------- exit method -----------") ;}@ overridepublic void before (object [] ARGs) {system. out. println ("----- enter method ------");} public static void main (string [] ARGs) throws exception {testitf target = new testitfimpl (); // target object testitf proxy = new proxyutil <testitf> (target ). getproxy (); // proxy of the target proxy instance. say ("wengjiabao"); // normal call // abnormal call proxy. say (null) ;}// output: ----- enter method ------ hell, wengjiabao ---------- exit method ---------------- enter method ------ call method error: Java. lang. reflect. invocationtargetexception