zoukankan      html  css  js  c++  java
  • cglib动态代理

    cglib实现原理:生成被代理对象的子类,使用ASM字节码技术重组来重写父类(被代理对象)的方法。生成的这个新对象,可以强制转换为被代理对象。也就是子类引用赋值给父类!

    案例编写:

    1. 导入cglib依赖包

      <dependencies>
        <!-- https://mvnrepository.com/artifact/cglib/cglib -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>
      </dependencies>

    2. 实现cglib动态代理类

    import java.lang.reflect.Method;
    import net.sf.cglib.proxy.Enhancer;
    import net.sf.cglib.proxy.MethodInterceptor;
    import net.sf.cglib.proxy.MethodProxy;
    
    public class MyCglibProxy implements MethodInterceptor{
        
        /**
         * @param clazz   被代理的类
         */
        public Object getInstance(Class clazz) throws Exception{
            //1. 创建字节码增强器,用来对被代理的类扩展。
            Enhancer enhancer = new Enhancer();
            //2. 告诉cglib,生成的子类需要继承那个父类。
            enhancer.setSuperclass(clazz);
            //3. 设置回调
            enhancer.setCallback(this);
            //4. 生成源代码,编译成class文件,加载到jvm,并返回代理对象。
             Object obj = enhancer.create();
             return obj;
        }
        /**
         * @param obj    生成的子类  
         * @param method 被拦截的方法
         * @param args   被拦截方法的参数
         * @param proxy  触发父类的方法对象
         */
        @Override
        public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
            System.out.println("正在进行挑选 ... ");
            proxy.invokeSuper(obj, args);
            System.out.println("包裹已发出,正在派送中!");
            return null;
        }
    }

    3. 创建被代理类

    public class Clothes {
        public void buyClothes() {
            System.out.println("我要买衣服");
        }
    }
    
    
    public class Computer {
        public void buyComputer() {
            System.out.println("我要买电脑");
        }
    }

    4. 测试

    public class Test {
    
        public static void main(String[] args) {
            try {
                Clothes obj = (Clothes)new MyCglibProxy().getInstance(Clothes.class);
                obj.buyClothes();
                System.out.println("====================");
                Computer obj2 = (Computer)new MyCglibProxy().getInstance(Computer.class);
                obj2.buyComputer();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    ==================
    【控制台输出】
            正在进行挑选 ... 
            我要买衣服
            包裹已发出,正在派送中!
            ====================
            正在进行挑选 ... 
            我要买电脑
            包裹已发出,正在派送中!
               
  • 相关阅读:
    jquery toggle(listenerOdd, listenerEven)
    struts quick start
    hdu 1518 Square (dfs)
    hdu 2544 最短路 (最短路径)
    hdu 1754 I Hate It (线段树)
    hdu 1856 More is better (并查集)
    hdu 1358 Period (KMP)
    hdu 2616 Kill the monster (DFS)
    hdu 2579 Dating with girls(2) (bfs)
    zoj 2110 Tempter of the Bone (dfs)
  • 原文地址:https://www.cnblogs.com/wlwl/p/10034059.html
Copyright © 2011-2022 走看看