0

JAVA分享篇(30)基础知识大总结

 1 year ago
source link: https://www.laiketui.com/104161.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

JAVA分享篇(30)基础知识大总结

行业动态 2023年1月12日 17:48 9

RPC动态代理类
package cn.itsource.consumer;

import java.lang.reflect.Proxy;

/**
* @author : pankun
* @Version : 1.0
**/
public class RpcProxy {

public static <T> T invoke(Class<T> clazz){
Class<?> [] interfaces = clazz.isInterface() ?
new Class[]{clazz} :
clazz.getInterfaces();
T result = (T) Proxy.newProxyInstance(clazz.getClassLoader(),interfaces,new RpcClient(clazz));
return result;
}

}
客户端启动类
package cn.itsource.consumer;

import cn.itsource.provider.ShopService;

/**
* @author : pankun
* @Version : 1.0
**/
public class IRpcConsumer {

public static void main(String[] args) {
ShopService invoke1 = RpcProxy.invoke(ShopService.class);
System.out.println(invoke1.getShopInfoById(1));
System.out.println(invoke1.getShopInfoById(2));
System.out.println(invoke1.getShopInfoById(3));
}
}
免责声明:部分文章信息来源于网络以及客户意见反馈,本站只负责对文章进行整理、排版、编辑,出于传递更多信息之目的,并不意味着赞同其观点或证实其内容的真实性,如本站文章和转稿涉及版权等问题,请作者在及时联系本站,我们会尽快联系您处理


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK