• JDK动态代理


    JDK动态代理又忘了,已经学习第四遍了,最简单的实现如下。

    package com.houjun.JDKDynamicProxy;
    
    /**
     * @Author: HouJun
     * @Date: 2019/10/10 9:11
     * @Description: 接口
     * @version: 1.0
     */
    public interface Subject {
        void doSomething();
    }
    package com.houjun.JDKDynamicProxy;
    
    /**
     * @Author: HouJun
     * @Date: 2019/10/10 9:11
     * @Description: 被代理类
     * @version: 1.0
     */
    public class ImplSubject implements Subject{
        @Override
        public void doSomething() {
            System.out.println("被代理类执行了");
        }
    }
    package com.houjun.JDKDynamicProxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    /**
     * @Author: HouJun
     * @Date: 2019/10/10 9:13
     * @Description: com.houjun.JDKDynamicProxy
     * @version: 1.0
     */
    public class Test {
        public static void main(String[] args) {
            ImplSubject implSubject = new ImplSubject();
    
            Subject s = (Subject) Proxy.newProxyInstance(implSubject.getClass().getClassLoader(), implSubject.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("");
                    Object invoke = method.invoke(implSubject, args);
                    System.out.println("");
                    return invoke;
                }
            });
            s.doSomething();
        }
    }
  • 相关阅读:
    kafka 启动停止
    kafka消息长度限制
    python给回调函数传参数
    Promise封装setTimeout
    Twisted 基础
    kafka-eagle
    go安装
    python asyncio
    ajv参数验证
    sequlizejs学习笔记整理
  • 原文地址:https://www.cnblogs.com/houj/p/11645927.html
Copyright © 2020-2023  润新知