/**
* 通过方法名称调用对应的方法
* @throws Exception
*/
public void test() throws Exception{
//需要调用方法所在的类
String className = "com.shuai.controller.SysRoleController";
Class clazz = Class.forName(className);
//获取所在类的对象
//Object obj = clazz.newInstance(); 提示过期
Object obj = clazz.getDeclaredConstructor().newInstance();
// selectPage1 调用的方法名
// 参数 String
Method method1 = clazz.getMethod("selectPage1",String.class);
method1.invoke(obj,"可以进行传参");
}
public class TestClass {
@Resource
private TestService testService;
public static TestClass testClass;
@PostConstruct
public void init() {
testClass = this;
}
/**
* 需要调用的方法
* @param value
*/
public void selectPage(String value) {
System.out.println(value + "=========");
IPage<SysRoleVo> list = testClass.testService.selectPage(new SysRoleQuery());
System.out.println(list + "=========");
}
}
返回结果: