One: the principal person
1 Packagecom.yeepay.porxy.cglib.test;2 3 ImportJava.util.HashMap;4 ImportJava.util.Map;5 6 /**7 * Target class, (delegate Class)8 * @authorShangxiaofei9 *Ten */ One Public classPaymentserver { A - PublicMap<string, string>Paymoney (String name,integer money) { -System.out.println ("Paymentserver.paymoney () Pay =========> name" "+name+" ", money=" "+money+" ""); theMap<string, string> map=NewHashmap<string, string>(); -Map.put ("Result", "payment has been completed"); - returnmap; - } +}
View Code
II: Enhancer (proxy Class)
1 Packagecom.yeepay.porxy.cglib.test;2 3 ImportJava.lang.reflect.Method;4 5 ImportNet.sf.cglib.proxy.MethodInterceptor;6 ImportNet.sf.cglib.proxy.MethodProxy;7 /**8 * Enhanced Class9 * @authorShangxiaofeiTen * One */ A Public classPoxyforserviceImplementsmethodinterceptor{ - - @Override the PublicObject Intercept (Object arg0, Method arg1, object[] arg2,methodproxy arg3)throwsThrowable { - //code for enhanced functionality -System.out.println ("poxyforservice.intercept (before method execution ========> sending messages to statistics)"); - if(arg2!=NULL&&arg2.length>0){ + for(inti=0;i<arg2.length;i++){ -System.out.println ("poxyforservice.intercept () sent parameter contents ====>" +arg2[i]); + } A } at - - //Execution Method Content -Object object=Arg3.invokesuper (arg0, arg2); - - inSystem.out.println ("poxyforservice.intercept (execution =======> calculation time after method execution)"); - returnobject; to } + - the *}
View Code
Third: the creation of agents of the factory class
1 Packagecom.yeepay.porxy.cglib.test;2 3 ImportNet.sf.cglib.proxy.Enhancer;4 5 /**6 * Agent Implementation factory class7 * @authorShangxiaofei8 *9 */Ten Public classPoxyfactory { One Public Staticpaymentserver Getpaymentserver () { A //Cglib Core class, generating proxy objects -Enhancer enhancer=Newenhancer (); - //class object for a delegate class that needs to be proxied into a core class object theEnhancer.setsuperclass (Paymentserver.class); - //class object that is put in the enhanced function for the core class object -Enhancer.setcallback (NewPoxyforservice ()); - //gets the proxy object for the delegate class from the core class object +Object object=enhancer.create (); - + return(Paymentserver) object; A } at}
View Code
Four: Test class
1 Packagecom.yeepay.porxy.cglib.test;2 3 ImportJava.util.Map;4 /**5 * Test Class6 * @authorShangxiaofei7 *8 */9 Public classTestController {Ten One PrivatePaymentserver paymentserver=poxyfactory.getpaymentserver (); A - Public voidpayment () { -System.out.println ("Testcontroller.payment () Start"); theMap<string, string> Map=paymentserver.paymoney ("Monster Lake", 100); -System.out.println ("Testcontroller.payment () End ===>" +map.get ("result")); - } - + - /** + * testcontroller.payment () Start A * Poxyforservice.intercept (Method execution before execution ========> send messages to statistics) at * poxyforservice.intercept () send the parameter content ====> Monster Lake - * poxyforservice.intercept () send the parameter content ====>100 - * Paymentserver.paymoney () Pay =========> name "Monster Lake", money= "100" - * poxyforservice.intercept (Execution of =======> calculation time after method execution) - * Testcontroller.payment () End of ===> has been completed paying - * @paramargs in */ - Public Static voidMain (string[] args) { toTestController testcontroller=NewTestController (); + testcontroller.payment (); - } the}
View Code
Spring Source Learning "Preparation" cglib Dynamic Proxy Example