zoukankan      html  css  js  c++  java
  • 动态代理Proxy和Cglib

     大概过程

     程序运行期间动态生成字节码文件,然后加载到内存里面,生成代理对象

     静态代理的时候,代理类和被代理类必须实现同一个接口,

     动态代理代码

    public class MyCalculator implements Calculator{
    
        @Override
        public int add(int i, int j){
            int result = i + j;
            return result;
        }
    
        @Override
        public int sub(int i, int j){
            int result = i - j;
            return result;
        }
    
        @Override
        public int mul(int i, int j){
            int result = i * j;
            return result;
        }
    
        @Override
        public int div(int i, int j){
            int result = i / j;
            return result;
        }
    }
    
    public class CalculatorProxy{
        public static Calculator getProxy(){
            // 获取类加载器
            ClassLoader classLoader = calculator.getClass().getClassLoader();
            // 获取要实现的接口
            Class<?> interfaces = calculator.getClass().getInterfaces();
            InvocationHandler h = new InvocationHandler(){
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
                    try{
                        Object result = method.invoke(calculator, args);
                        return result;
                    }catch(Exception e){
                        e.printStackTrace();
                    }                
                }
            }
            Proxy.newProxyInstance();
        }
    }
    
    public class Test{
        public static void main(String[] args){
            Calculator proxy = CalculatorProxy.getProxy(new MyCalculator());
            System.out.println(proxy.add(1,1));
            System.out.println(proxy.getClass());
        }
    }

     ASM是Java字节码操控框架,动态生成类或增强既有类的功能

     ASM可以直接产生二进制class文件,可以在类被加载前动态改变类的行为

     CGlib

    public class MyCalculator{
    
        public int add(int i, int j){
            int result = i + j;
            return result;
        }
        
        public int sub(int i, int j){
            int result = i - j;
            return result;
        }
    
        public int mul(int i, int j){
            int result = i * j;
            return result;
        }
    
        public int div(int i, int j){
            int result = i / j;
            return result;
        }
    }
    
    public class MyCglig implements MethodInterceptor{
        @Override
        public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable{
            System.out.println("输出前");
            Object o = proxy.invokeSuper(obj, args);
            System.out.println("输出后");
            return o;
        }
    }
    
    public class Test{
        public static void main(String[] args){
            // 动态代理创建的class文件存储到本地
            System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "d:\code");
            // 创建cglib获取代理对象的操作对象
            Enhancer enhancer = new Enhancer();
            // 设置enhancer对象的父类
            enhancer.setSuperclass(MyCalculator.class);
            // 设置enhancer的回调对象
            enhancer.setCallback(new MyCglib());
            // 创建代理对象
            Object o = enchancer.create();
            MyCalculator MyCalculator = (MyCalculator)enhancer.create();
            MyCalculator.add(1, 1);
            System.out.println(MyCalculator.getClass());        
        }
    }
  • 相关阅读:
    Spring Boot 使用mysql数据库
    Nginx开启Gzip压缩大幅提高页面加载速度
    构建微服务:Spring boot 入门篇
    Python时间戳和日期的相互转换
    【Mongodb】aggregate限制返回字段
    MongoDB的skip,limit,sort执行顺序
    结对-结对四则运算生成器-最终程序
    C# List分页
    c# List的排序
    C#并行编程-Parallel
  • 原文地址:https://www.cnblogs.com/YC-L/p/14240478.html
Copyright © 2011-2022 走看看