Java Dynamic proxy Factory

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.