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();
        }
    
    }
    

      

  • 相关阅读:
    401. Binary Watch
    46. Permutations
    61. Rotate List
    142. Linked List Cycle II
    86. Partition List
    234. Palindrome Linked List
    19. Remove Nth Node From End of List
    141. Linked List Cycle
    524. Longest Word in Dictionary through Deleting
    android ListView详解
  • 原文地址:https://www.cnblogs.com/jqlbj/p/6748290.html
Copyright © 2011-2022 走看看