PackageCom.stono.cglib;ImportJava.lang.reflect.Method;ImportNet.sf.cglib.proxy.Enhancer;ImportNet.sf.cglib.proxy.MethodInterceptor;ImportNet.sf.cglib.proxy.MethodProxy; Public classCglibproxyImplementsMethodinterceptor {Privateenhancer enhance =Newenhancer (); PublicObject GetProxy (class<?>clazz) { //set class to create subclassesEnhance.setsuperclass (Clazz); Enhance.setcallback ( This); returnenhance.create (); } /*** Interception of calls to all target class methods <br> * Instance of obj target class <br> * Reflection object for M target method <br> * Parameters of the Args method <br> * Example of proxy class*/@Override PublicObject Intercept (Object obj, Method m, object[] args, methodproxy proxy)throwsthrowable {System.out.println ("Before ..."); //the proxy class calls the method of the parent classObject Invokesuper =proxy.invokesuper (obj, args); System.out.println ("After ..."); returnInvokesuper; } /** * @paramargs*/ Public Static voidMain (string[] args) {Cglibproxy proxy=NewCglibproxy (); Train T= (Train) proxy.getproxy (Train.class); T.move (); }}/*** The class being represented **/classTrain { Public voidMove () {System.out.println ("Train is running!"); }}
Cglib jar package on the Internet to find, can not find eclipse such as inside, there is security problem;
Cglib Generating Dynamic agents