实现CGLIB代理

package cglibProxy;

import DaoImpl.CustomerImpl;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * 基于CGLIB动态代理
 *
 * @author jia
 * @create
 */
public class CGLibProxy {
    private Object targetObject;

    private Object createProxy(Object obj) {
        targetObject = obj;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(targetObject.getClass());
        enhancer.setCallback(new MyHandler());
        return enhancer.create();
    }

    class MyHandler implements MethodInterceptor {
        public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
            System.out.println("开始处理。。。");
            Object returnValue = method.invoke(targetObject, objects);
            System.out.println("结束处理。。。");
            return returnValue;
        }
    }

    public static void main(String[] args) {
        CGLibProxy cgLibProxy = new CGLibProxy();
        CustomerImpl customerImpl = (CustomerImpl) cgLibProxy.createProxy(new CustomerImpl());
        customerImpl.shopping();
    }

}

  

posted @ 2017-04-22 16:56  ゞ清茶℡  阅读(98)  评论(0编辑  收藏  举报