我必须得将mapper作为参数来进行传递,在进行反射调用方法的时候,就会报错。请问这种情况我该如何处理,才能够实现这一要求了?
@Configuration
@EnableScheduling
public class aaa {
// Mapper是个接口
@Autowired
private TestMapper testpMapper;
@Scheduled(cron = "0/5 * * * * ?")
private void createOrderTableTime() {
// try {
// determineCreateTable((Class<? extends BaseMapper>) Class.forName(testMapper.getClass().getName()));
determineCreateTable(testMapper.getClass());
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
}
// public void determineCreateTable(Class<? extends BaseMapper> tableMapper) {
public void determineCreateTable(Class tableMapper) {
try {
Method method = tableMapper.getDeclaredMethod("count", String.class);
// 这种是可行的
// int count = (int) method.invoke(testMapper, "100001");
// int count = (int) method.invoke(tableMapper, "100001");
// 在进行实例化的时候会产生错误
int count = (int) method.invoke(tableMapper.newInstance(), "100001");
System.out.println(count);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
}
}
报错信息如下:
java.lang.IllegalArgumentException: object is not an instance of declaring class
java.lang.InstantiationException: com.sun.proxy.$Proxy88