我无法使用Controller和JdbcTemplate在Spring启动中使用Java反射。
默认控制器如下所示:
public class DefaultController {
private static final Logger logger = LoggerFactory.getLogger(DefaultController.class);
public JsonResponseDataModel selectOneAuto(Long id, Class<?> repository, HttpServletResponse response){
final JsonResponseDataModel result = new JsonResponseDataModel();
System.out.println("The name of class is " + repository.getName());
Method[] methods = repository.getMethods();
for (Method method : methods) {
System.out.println("Method: " + method.getName());
}
try {
//Method method = repository.getClass().getMethod("selectOne", Long.class);
Method method = repository.getClass().getDeclaredMethod("selectOne", Long.class);
method.invoke(repository, id);
logger.info("selectOneAuto : id={} ", id);
} catch (EmptyResultDataAccessException e) {
result.setEmptyResultDataAccessException("id", id.toString());
} catch (DataAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return result;
}
}
在CompanyRepository类内部定义了带有Long输入的selectOne方法:
@Transactional(readOnly=true)
public CompanyModel selectOne(Long id) {
CompanyModel result = null;
final String sql = "SELECT * FROM company WHERE id=?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new CompanyRowMapper());
}
当我创建一个新类“ CompanyController extended DefaultController”并调用方法selectOneAuto时:
selectOneAuto(id, new CompanyRepository().getClass(), response);
然后,在“方法方法= repository.getClass()。getDeclaredMethod(“ selectOne”,Long.class);“行上以错误结尾
"Java.lang.NoSuchMethodException: java.lang.Class.selectOne(java.lang.Long)"
但是“ selectOneAuto”方法内部的for循环输出名为“ selectOne”的方法。怎么了
您的代码尝试在的实例上调用方法Class
。
method.invoke(存储库,ID);
Class
因为您将new CompanyRepository().getClass()
作为参数传递,所以存储库对象是的实例。
要注意的第二点是存储库已经是Class的实例,因此不需要在此对象上调用getClass()。
您应该使用以下代码获取method对象:
Method method = repository.getDeclaredMethod("selectOne", Long.class);
然后这应该工作:
CompanyRepository repsitoryObj = new CompanyRepository();
method.invoke(repsitoryObj, id);
或更好,更清洁的方法是仅更改repository
参数的类型,因为CompanyRepository
方法将如下所示:
public JsonResponseDataModel selectOneAuto(Long id, CompanyRepository repository, HttpServletResponse response){
final JsonResponseDataModel result = new JsonResponseDataModel();
System.out.println("The name of class is " + repository.getClass().getName());
Method[] methods = repository.getClass().getMethods();
for (Method method : methods) {
System.out.println("Method: " + method.getName());
}
try {
//Method method = repository.getClass().getMethod("selectOne", Long.class);
Method method = repository.getClass().getDeclaredMethod("selectOne", Long.class);
method.invoke(repository, id);
logger.info("selectOneAuto : id={} ", id);
} catch (EmptyResultDataAccessException e) {
result.setEmptyResultDataAccessException("id", id.toString());
} catch (DataAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return result;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句