public static void main(String[] args) throws Exception {
String className = null;
//服务方接口和实现类所在的包路径
Class superClass=Class.forName("cn.zhangxueliang.rpc.server.HelloRPC");
Reflections reflections = new Reflections("cn.zhangxueliang.rpc.server");
//得到某接口下的所有实现类
Set<Class> ImplClassSet=reflections.getSubTypesOf(superClass);
if(ImplClassSet.size()==0){
System.out.println("未找到实现类");
}else if(ImplClassSet.size()>1){
System.out.println("找到多个实现类,未明确使用哪一个");
}else {
//把集合转换为数组
Class[] classes=ImplClassSet.toArray(new Class[0]);
className=classes[0].getName(); //得到实现类的名字
}
Object clazz = Class.forName(className).newInstance();
Method method = clazz.getClass().getMethod("hello", new Class[]{String.class});
Object result = method.invoke(clazz, new Object[]{"张学亮"});
System.out.println(result);
}