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();
    }
}
01-13 15:05