diff --git a/basic-util/pom.xml b/basic-util/pom.xml index fde7105e..01bd8d6f 100644 --- a/basic-util/pom.xml +++ b/basic-util/pom.xml @@ -152,6 +152,13 @@ 1.0-SNAPSHOT + + + + cglib + cglib + + \ No newline at end of file diff --git a/basic-util/src/main/java/ink/wgink/util/remote/rest/RestRemoteRequestDemo.java b/basic-util/src/main/java/ink/wgink/util/remote/rest/RestRemoteRequestDemo.java new file mode 100644 index 00000000..591b4197 --- /dev/null +++ b/basic-util/src/main/java/ink/wgink/util/remote/rest/RestRemoteRequestDemo.java @@ -0,0 +1,45 @@ +package ink.wgink.util.remote.rest; + +import net.sf.cglib.proxy.Enhancer; +import net.sf.cglib.proxy.MethodInterceptor; +import net.sf.cglib.proxy.MethodProxy; + +import java.lang.reflect.Method; + +/** + * @ClassName: RestRemoteRequest + * @Description: rest远程请求 + * @Author: wanggeng + * @Date: 2021/9/18 4:23 下午 + * @Version: 1.0 + */ +public class RestRemoteRequestDemo implements MethodInterceptor { + + + @Override + public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { + return methodProxy.invokeSuper(o, args); + } + + public static class RestRemoteRequestFactory { + public static Object getProxy(Class clazz) { + Enhancer enhancer = new Enhancer(); + enhancer.setClassLoader(clazz.getClassLoader()); + enhancer.setSuperclass(clazz); + enhancer.setCallback(new RestRemoteRequestDemo()); + return enhancer.create(); + } + } + + public static class Demo { + void say(String msg) { + System.out.println(msg); + } + } + + public static void main(String[] args) { + Demo demo = (Demo) RestRemoteRequestFactory.getProxy(Demo.class); + demo.say("你好"); + } + +} diff --git a/basic-util/src/main/java/ink/wgink/util/remote/rest/handler/RestRemoteHandler.java b/basic-util/src/main/java/ink/wgink/util/remote/rest/handler/RestRemoteHandler.java new file mode 100644 index 00000000..7e31f3d4 --- /dev/null +++ b/basic-util/src/main/java/ink/wgink/util/remote/rest/handler/RestRemoteHandler.java @@ -0,0 +1,26 @@ +package ink.wgink.util.remote.rest.handler; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + +/** + * @ClassName: RestRemoterHandler + * @Description: Rest远程调用 + * @Author: wanggeng + * @Date: 2021/9/18 5:14 下午 + * @Version: 1.0 + */ +public class RestRemoteHandler implements InvocationHandler { + + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + System.out.println(method.getDeclaringClass().getClassLoader()); + // 否则执行统一接口处理方法 + return method.invoke(this, args); + } + + public Object run() { + return "run"; + } + +} diff --git a/basic-util/src/main/java/ink/wgink/util/remote/rest/proxy/RestRemoteProxy.java b/basic-util/src/main/java/ink/wgink/util/remote/rest/proxy/RestRemoteProxy.java new file mode 100644 index 00000000..d351c061 --- /dev/null +++ b/basic-util/src/main/java/ink/wgink/util/remote/rest/proxy/RestRemoteProxy.java @@ -0,0 +1,38 @@ +package ink.wgink.util.remote.rest.proxy; + +import ink.wgink.util.remote.rest.handler.RestRemoteHandler; + +import java.lang.reflect.Proxy; + +/** + * @ClassName: RestRemoteProxy + * @Description: Rest远程调用代理 + * @Author: wanggeng + * @Date: 2021/9/18 5:40 下午 + * @Version: 1.0 + */ +public class RestRemoteProxy { + + public static T getInstance(Class cls) { + RestRemoteHandler restRemoteHandler = new RestRemoteHandler(); + Object o = Proxy.newProxyInstance(cls.getClassLoader(), new Class[]{cls}, restRemoteHandler); + return (T) o; + } + + public interface IDemo { + String say(); + } + + public static class DemoImpl implements IDemo { + @Override + public String say() { + return "go"; + } + } + + public static void main(String[] args) { + IDemo demo = RestRemoteProxy.getInstance(IDemo.class); + System.out.println(demo.say()); + } + +}