import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class ProxyHandler<T> implements InvocationHandler { private T target; public ProxyHandler(T target) { this.target = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("before invoke ."); Object ret = method.invoke(target, args); System.out.println("after invoke ."); return ret; } public T getProxy() { return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), target.getClass().getInterfaces(), this); } }
public interface UserService { String echo(String content); }
import sun.misc.ProxyGenerator; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class UserServiceImpl implements UserService { @Override public String echo(String content) { return content + "_impl"; } public static void main(String[] args) throws IOException { UserService us = new UserServiceImpl(); ProxyHandler<UserService> proxyHandler = new ProxyHandler<>(us); UserService userService = proxyHandler.getProxy(); System.out.println(userService.echo("hello java ")); System.out.println(userService.getClass()); generateProxyClassToFile(); } public static void generateProxyClassToFile() throws IOException { byte[] bts = ProxyGenerator.generateProxyClass("$Proxy0", new Class[]{UserService.class}); File file = new File("./", "$Proxy0.class"); if (!file.exists()) { file.createNewFile(); } FileOutputStream fos = new FileOutputStream(file); fos.write(bts); fos.flush(); fos.close(); } }
代理类的继承接口以及成员变量:
//生成的代理类字节码: import com.javartisan.springboot.javartisanspringboot.aop.UserService; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.lang.reflect.UndeclaredThrowableException; public final class $Proxy0 extends Proxy implements UserService { private static Method m1; private static Method m3; private static Method m2; private static Method m0; public $Proxy0(InvocationHandler var1) throws { super(var1); } public final boolean equals(Object var1) throws { try { return (Boolean)super.h.invoke(this, m1, new Object[]{var1}); } catch (RuntimeException | Error var3) { throw var3; } catch (Throwable var4) { throw new UndeclaredThrowableException(var4); } } public final String echo(String var1) throws { try { // 代理实现的关键地方,h是继承至Proxy return (String)super.h.invoke(this, m3, new Object[]{var1}); } catch (RuntimeException | Error var3) { throw var3; } catch (Throwable var4) { throw new UndeclaredThrowableException(var4); } } public final String toString() throws { try { return (String)super.h.invoke(this, m2, (Object[])null); } catch (RuntimeException | Error var2) { throw var2; } catch (Throwable var3) { throw new UndeclaredThrowableException(var3); } } public final int hashCode() throws { try { return (Integer)super.h.invoke(this, m0, (Object[])null); } catch (RuntimeException | Error var2) { throw var2; } catch (Throwable var3) { throw new UndeclaredThrowableException(var3); } } static { try { m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object")); m3 = Class.forName("com.javartisan.springboot.javartisanspringboot.aop.UserService").getMethod("echo", Class.forName("java.lang.String")); m2 = Class.forName("java.lang.Object").getMethod("toString"); m0 = Class.forName("java.lang.Object").getMethod("hashCode"); } catch (NoSuchMethodException var2) { throw new NoSuchMethodError(var2.getMessage()); } catch (ClassNotFoundException var3) { throw new NoClassDefFoundError(var3.getMessage()); } } }
原文:https://www.cnblogs.com/leodaxin/p/10369646.html