通过反射获得接口的实现类的全路径,然后根据全路径得到class对象,调用对象的某个方法。
1:反射获得某个接口的全路径
try{
List<String> cacheClassList = Lists.newArrayList();
//根据接口类型返回相应的所有bean
Map<String, CommandLineRunner> map = applicationContext.getBeansOfType(CommandLineRunner.class);
for (String key:map.keySet()){
CommandLineRunner commandLineRunner = map.get(key);
String className = commandLineRunner.getClass().getName();
cacheClassList.add(className);
}
logger.debug("缓存列表查询结束");
resultVO.setTotal(cacheClassList.size());
return resultVO.success(cacheClassList);
}catch (Exception e){
logger.error("缓存查询失败:",e);
return resultVO.fail("缓存列表查询失败");
}
2:通过全路径调用某个接口的方法
try{
Class clazz = Class.forName(className);
Method method = clazz.getMethod("initData",ApplicationContext.class);
Object object = clazz.newInstance();
method.invoke(object,applicationContext);
return resultVO.success();
}catch (Exception e){
logger.error("刷新缓存失败:",e);
return resultVO.fail("刷新缓存失败!");
}
调用的方法: