zoukankan      html  css  js  c++  java
  • 实现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();
        }
    
    }
    

      

  • 相关阅读:
    理解原型Prototype、继承
    解决js跨域问题的基本方法之 -------JSONP
    CSS3中动画效果Transitions与Animations的区别
    支付宝支付实例
    上线实例
    Celery
    Redis
    git
    jwt认证
    登录认证
  • 原文地址:https://www.cnblogs.com/jqlbj/p/6748290.html
Copyright © 2011-2022 走看看