中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

cglib實現(xiàn)動態(tài)代理構(gòu)建帶參數(shù)的代理實例

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
cglib實現(xiàn)動態(tài)代理構(gòu)建帶參數(shù)的代理實例:
 
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
 
/**
 * Created by Carl on 14-12-30.
 */
public class CglibProxy implements MethodInterceptor{
    /**
     * 創(chuàng)建代理對象方法
     *
     * @param target        代理對象
     * @param args          對應的構(gòu)造器參數(shù)類型
     *
     *                          例:有構(gòu)造器如下
     *                          public Person(name,age){...} name為String.class age為int.class
     *                          寫入name的類型與age的類型
     *
     *                          則:new Class[]{String.class,int.class}
     *
     * @param argsValue     對應的構(gòu)造器參數(shù)值
     *
     *                          例:如此創(chuàng)建對象 new Person("name",23) 用以下方式傳入:new Object[]{"name",23}
     *
     * @param <T>           <泛型方法>
     * @return              返回跟代理對象類型
     */
    public <T> T getInstance(T target,Class[] args,Object[] argsValue){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        return (T) enhancer.create(args,argsValue);
    }
 
    /**
     * 創(chuàng)建代理對象方法
     *
     * @param target        代理對象
     * @param <T>           <泛型方法>
     * @return              返回跟代理對象類型
     */
    public <T> T getInstance(T target){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        return (T) enhancer.create();
    }
 
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        Object result = null;
        try{
            System.out.println("Before "+method.getName()+" ..");
            result = methodProxy.invokeSuper(o,objects);
            System.out.println("End "+method.getName()+" ..");
        }catch(Exception e){
            System.out.println("Errod "+method.getName()+" ..");
        }
        return result;
    }
}

需引入 cglib-2.1.3.jar 和 asm.jar

標簽:

版權(quán)申明:本站文章部分自網(wǎng)絡,如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Python實現(xiàn)HTTP代理服務器

下一篇:php生成隨機字符串函數(shù)(支持字母大小寫,數(shù)字,中文)