CGlib動態代理複習

qq_35900437發表於2017-12-24

CGlib動態代理複習

1.要點:

A.代理類實現MethodInterceptor介面 B.複寫incept方法; C.使用單例模式進行編寫;D.Enhancer.create方法的使用

2.口訣:攔 攔 貸 單
3.Code Show

 */
public class CGlibProxy implements MethodInterceptor【攔】 {

    private static CGlibProxy instance=new CGlibProxy();

    private CGlibProxy() 【單】{

    }


    public static CGlibProxy getInstance(){
        return instance;
    }

    public <T> T getProxy(Class<T> cls){
        return (T) Enhancer.create(cls,this);
    }


    @Override
    public Object intercept【攔】(Object o, Method method, Object[] objects, MethodProxy proxy) throws Throwable {

        before();
        Object result = proxy.invokeSuper(o, objects);【貸】
        after();
        return result;
    }

    public void after() {
        System.out.println("前置方法");
    }

    public void before() {
        System.out.println("tango");
    }

}

相關文章