接口类定义
package com.example.demo.test;
public interface IApp {
void doSomething(String str);
}
接口类实现
package com.example.demo.test;
public class AppServiceImpl implements IApp {
@Override
public void doSomething(String str) {
System.out.println(str);
}
}
调用处理器
package com.example.demo.test;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class AppProxyHandler implements InvocationHandler {
private AppServiceImpl appService;
public AppServiceImpl getAppService() {
return appService;
}
public AppProxyHandler setAppService(AppServiceImpl appService) {
this.appService = appService;
return this;
}
public AppProxyHandler(AppServiceImpl appService) {
this.appService = appService;
}
public AppProxyHandler() {
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(args);
System.out.println("before");
Object invoke = method.invoke(this.appService, args);
System.out.println("after");
return invoke;
}
}
代码运行
package com.example.demo.test;
import java.lang.reflect.Proxy;
public class ProxyTest {
public static void main(String[] args) {
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
AppServiceImpl appService = new AppServiceImpl();
IApp proxyInstance = (IApp) Proxy.newProxyInstance(appService.getClass().getClassLoader(), appService.getClass().getInterfaces(), new AppProxyHandler(appService));
IApp proxyInstance2 = (IApp) Proxy.newProxyInstance(appService.getClass().getClassLoader(), appService.getClass().getInterfaces(), new AppProxyHandler2(appService));
proxyInstance.doSomething("hello proxy");
System.out.println(proxyInstance.getClass());
System.out.println(proxyInstance2.getClass());
}
}
生成的动态代理类
package com.sun.proxy;
import com.example.demo.test.IApp;
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 IApp {
private static Method m1;
private static Method m2;
private static Method m3;
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 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 void doSomething(String var1) throws {
try {
super.h.invoke(this, m3, new Object[]{var1});
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
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"));
m2 = Class.forName("java.lang.Object").getMethod("toString");
m3 = Class.forName("com.example.demo.test.IApp").getMethod("doSomething", Class.forName("java.lang.String"));
m0 = Class.forName("java.lang.Object").getMethod("hashCode");
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw new NoClassDefFoundError(var3.getMessage());
}
}
}
参考文档:
填坑之动态代理生成的代理类文件在哪?
- 查清楚自己的JDK版本,不然再怎么操作也看不到!!!
- Main方法中,Test的Junit单元测试中不会出现,不用试了,我就是调到这个坑了!!!
- 文件位置为跟目录的同级目录!!!
试图输出 JDK 动态代理所产生的 .class 文件时可能踩到的坑
不要 在 JUnit 的单元测试方法中写测试代码,而应该写在 main 方法中
在指定位置生成动态代理的.class文件
System.setProperty(“sun.misc.ProxyGenerator.saveGeneratedFiles”,“true”);//生成的字节码文件保存在working directory