结帖率100%,反射,动态获取类型m.invoke(o, ParamType); m.invoke(o, ParamType);调用java反射进行注入参数时ParamType没有一个好的方法动态确定,spring钻的比较深的大侠,指点迷津啊。3Q 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java.lang.reflect.Method.getParameterTypes()可以得到所有参数的类型。 我的方法,你参考下:private Method getMethod(String bean, Integer paramLength) throws AppException { String methodName = bean.substring(bean.indexOf(".") + 1); String beanName = bean.substring(0, bean.indexOf(".")); //获得请求的bean String beanXml = InitParameter.getBeanXml(); if (beanXml.isEmpty()) { throw new AppException("获取Bean配置文件失败。beanXml=" + beanXml); } ClassPathResource resource = new ClassPathResource(beanXml); BeanFactory factory = new XmlBeanFactory(resource); ajxObj = (Object) factory.getBean(beanName); if (ajxObj == null) { throw new AppException("获取Bean对象失败。beanName=" + beanName); } //获得bean的方法集 Method[] method = ajxObj.getClass().getMethods(); if (method.length == 0) { throw new AppException("指定Bean对象没有方法:BeanName=" + beanName + ",method=" + methodName); } for (int i = 0; i < method.length; i++) { if (method[i].getName().equals(methodName)) { //匹配方法的参数和传入的参数 Class[] parameter = method[i].getParameterTypes(); int intParamLength = parameter.length; for (int j = 0; j < parameter.length; j++) { if (parameter[j] == HttpServletRequest.class) { intParamLength = intParamLength - 1; } else if (parameter[j] == HttpServletResponse.class) { intParamLength = intParamLength - 1; } } if (intParamLength == paramLength) { //方法名称正确,参数长度符合 return method[i]; } } } throw new AppException("没有查询到指定Bean对象的方法:BeanName=" + beanName + ",method=" + methodName+",参数个数:"+paramLength.toString()); } invoke的第二个参数不是ParamType,而是ParamObjects了,也就是说是一个实例数组你传入一个Object[]就行了,不管参数类型是什么,总可以将具体的参数实例加入Object[]的 当传入的参数数量和方法的数量相同的时候,还要匹配参数类型:private Object[] getArgs(Method method, List paraList,HttpServletRequest request,HttpServletResponse response) throws AppException{ Class[] parameter = method.getParameterTypes(); Object[] args = new Object[parameter.length]; for (int j = 0; j < parameter.length; j++) { try { if (parameter[j] == Map.class) { args[j] = (Map) paraList.get(j); }else if (parameter[j] == HashMap.class) { args[j] = (HashMap) paraList.get(j); }else if (parameter[j] == List.class) { args[j] = (List) paraList.get(j); }else if (parameter[j] == ArrayList.class) { args[j] = (ArrayList) paraList.get(j); }else if (parameter[j] == HttpServletRequest.class) { args[j] = (HttpServletRequest) request; } else if (parameter[j] == HttpServletResponse.class) { args[j] = (HttpServletResponse) response; } } catch (Exception e) { //返回错误,数据类型错误========================================= throw new AppException(ajaxBean + "的参数和传入的参数类型不符。参数序号:" + ((Integer) j).toString() + ",参数类型:" + parameter[j].toString() + ",传入类型:" + paraList.get(j).getClass()); } if (parameter[j] == String.class || parameter[j] == Boolean.class || parameter[j] == boolean.class || parameter[j] == Integer.class || parameter[j] == int.class || parameter[j] == float.class || parameter[j] == Float.class || parameter[j] == double.class || parameter[j] == Double.class || parameter[j] == Date.class) { Object value = paraList.get(j); //jsonList.get(j)是简单数据类型 if (value instanceof Map) { args[j] = getSingleMapValue((Map) value); } else if (value instanceof List) { args[j] = getSingleListValue((List) value); } else if (parameter[j] == String.class) { args[j] = (String) value; } else if (parameter[j] == Boolean.class || parameter[j] == boolean.class) { args[j] = (Boolean) value; } else if (parameter[j] == Integer.class || parameter[j] == int.class) { if (value instanceof Integer) { args[j] = value; } else if (value instanceof Float) { args[j] = ((Float) value).intValue(); } else if (value instanceof Double) { args[j] = ((Double) value).intValue(); } else if (value instanceof String) { args[j] = Integer.valueOf((String) value).intValue(); } else { //返回错误,数据类型错误========================================= throw new AppException(ajaxBean + "的参数和传入的参数类型不符。参数序号:" + ((Integer) j).toString() + ",参数类型:Integer,传入类型:" + value.getClass()); } } else if (parameter[j] == float.class || parameter[j] == Float.class) { if (value instanceof Float) { args[j] = value; } else if (value instanceof Integer) { args[j] = (Float) value; } else if (value instanceof Double) { args[j] = ((Double) value).floatValue(); } else if (value instanceof String) { args[j] = Float.valueOf((String) value).floatValue(); } else { throw new AppException(ajaxBean + "的参数和传入的参数类型不符。参数序号:" + ((Integer) j).toString() + ",参数类型:Float,传入类型:" + value.getClass()); } } else if (parameter[j] == double.class || parameter[j] == Double.class) { if (value instanceof Double) { args[j] = value; } else if (value instanceof Float) { args[j] = ((Float) value).doubleValue(); } else if (value instanceof Integer) { args[j] = ((Integer) value).doubleValue(); } else if (value instanceof String) { args[j] = Double.valueOf((String) value).doubleValue(); } else { throw new AppException(ajaxBean + "的参数和传入的参数类型不符。参数序号:" + ((Integer) j).toString() + ",参数类型:Double,传入类型:" + value.getClass()); } } else if (parameter[j] == Date.class) { if (value instanceof Date) { args[j] = Date.valueOf(value.toString()); } else if (value instanceof String) { args[j] = Date.valueOf(value.toString()); } else { //返回错误,数据类型错误========================================= throw new AppException(ajaxBean + "的参数和传入的参数类型不符。参数序号:" + ((Integer) j).toString() + ",参数类型:Date,传入类型:" + value.getClass()); } } } } return args; } Object refObj = objectPool.getBean(refId);//自定义的对象池方法if(refObj != null){ m.invoke(o, new Object[]{refObj});}如果按着这种数组方式来调用的话会报类型不匹配的异常java.lang.IllegalArgumentException: argument type mismatch 楼主能把你的代码全给出来吗,感觉不明不白的,本来多简单一个问题.2楼所说的能得到方法需要传入的参数的类型.知道类型后,传入相应类型的一个Object[]的参数就OK了. 关于CJK解码的问题 救急,工作需要,关于正则表达式 我想问一个比较基础的问题 请问这里的Outer.this.a 应该如何理解??? java3d编程遇到的问题 请问JButton的凸起是如何获得的? 构造方法VS getInstance()? 大家快来帮帮忙啊 求教。 有谁能说出这个表达式得运行结果? 根据省份分类,存到集合对象中,怎么实现 大伙看看这个怎么用正则
java.lang.reflect.Method.getParameterTypes()可以得到所有参数的类型。
String methodName = bean.substring(bean.indexOf(".") + 1);
String beanName = bean.substring(0, bean.indexOf(".")); //获得请求的bean
String beanXml = InitParameter.getBeanXml();
if (beanXml.isEmpty()) {
throw new AppException("获取Bean配置文件失败。beanXml=" + beanXml);
}
ClassPathResource resource = new ClassPathResource(beanXml);
BeanFactory factory = new XmlBeanFactory(resource);
ajxObj = (Object) factory.getBean(beanName);
if (ajxObj == null) {
throw new AppException("获取Bean对象失败。beanName=" + beanName);
} //获得bean的方法集
Method[] method = ajxObj.getClass().getMethods();
if (method.length == 0) {
throw new AppException("指定Bean对象没有方法:BeanName=" + beanName + ",method=" + methodName);
}
for (int i = 0; i < method.length; i++) {
if (method[i].getName().equals(methodName)) {
//匹配方法的参数和传入的参数
Class[] parameter = method[i].getParameterTypes(); int intParamLength = parameter.length;
for (int j = 0; j < parameter.length; j++) {
if (parameter[j] == HttpServletRequest.class) {
intParamLength = intParamLength - 1;
} else if (parameter[j] == HttpServletResponse.class) {
intParamLength = intParamLength - 1;
}
} if (intParamLength == paramLength) {
//方法名称正确,参数长度符合
return method[i];
}
}
}
throw new AppException("没有查询到指定Bean对象的方法:BeanName=" + beanName + ",method=" + methodName+",参数个数:"+paramLength.toString());
}
你传入一个Object[]就行了,不管参数类型是什么,总可以将具体的参数实例加入Object[]的
Class[] parameter = method.getParameterTypes();
Object[] args = new Object[parameter.length]; for (int j = 0; j < parameter.length; j++) {
try {
if (parameter[j] == Map.class) {
args[j] = (Map) paraList.get(j);
}else if (parameter[j] == HashMap.class) {
args[j] = (HashMap) paraList.get(j);
}else if (parameter[j] == List.class) {
args[j] = (List) paraList.get(j);
}else if (parameter[j] == ArrayList.class) {
args[j] = (ArrayList) paraList.get(j);
}else if (parameter[j] == HttpServletRequest.class) {
args[j] = (HttpServletRequest) request;
} else if (parameter[j] == HttpServletResponse.class) {
args[j] = (HttpServletResponse) response;
}
} catch (Exception e) {
//返回错误,数据类型错误=========================================
throw new AppException(ajaxBean + "的参数和传入的参数类型不符。参数序号:" + ((Integer) j).toString() + ",参数类型:" + parameter[j].toString() + ",传入类型:" + paraList.get(j).getClass());
} if (parameter[j] == String.class || parameter[j] == Boolean.class || parameter[j] == boolean.class || parameter[j] == Integer.class || parameter[j] == int.class || parameter[j] == float.class || parameter[j] == Float.class || parameter[j] == double.class || parameter[j] == Double.class || parameter[j] == Date.class) {
Object value = paraList.get(j); //jsonList.get(j)是简单数据类型
if (value instanceof Map) {
args[j] = getSingleMapValue((Map) value);
} else if (value instanceof List) {
args[j] = getSingleListValue((List) value);
} else if (parameter[j] == String.class) {
args[j] = (String) value;
} else if (parameter[j] == Boolean.class || parameter[j] == boolean.class) {
args[j] = (Boolean) value;
} else if (parameter[j] == Integer.class || parameter[j] == int.class) {
if (value instanceof Integer) {
args[j] = value;
} else if (value instanceof Float) {
args[j] = ((Float) value).intValue();
} else if (value instanceof Double) {
args[j] = ((Double) value).intValue();
} else if (value instanceof String) {
args[j] = Integer.valueOf((String) value).intValue();
} else {
//返回错误,数据类型错误=========================================
throw new AppException(ajaxBean + "的参数和传入的参数类型不符。参数序号:" + ((Integer) j).toString() + ",参数类型:Integer,传入类型:" + value.getClass());
}
} else if (parameter[j] == float.class || parameter[j] == Float.class) {
if (value instanceof Float) {
args[j] = value;
} else if (value instanceof Integer) {
args[j] = (Float) value;
} else if (value instanceof Double) {
args[j] = ((Double) value).floatValue();
} else if (value instanceof String) {
args[j] = Float.valueOf((String) value).floatValue();
} else {
throw new AppException(ajaxBean + "的参数和传入的参数类型不符。参数序号:" + ((Integer) j).toString() + ",参数类型:Float,传入类型:" + value.getClass());
}
} else if (parameter[j] == double.class || parameter[j] == Double.class) {
if (value instanceof Double) {
args[j] = value;
} else if (value instanceof Float) {
args[j] = ((Float) value).doubleValue();
} else if (value instanceof Integer) {
args[j] = ((Integer) value).doubleValue();
} else if (value instanceof String) {
args[j] = Double.valueOf((String) value).doubleValue();
} else {
throw new AppException(ajaxBean + "的参数和传入的参数类型不符。参数序号:" + ((Integer) j).toString() + ",参数类型:Double,传入类型:" + value.getClass());
}
} else if (parameter[j] == Date.class) {
if (value instanceof Date) {
args[j] = Date.valueOf(value.toString());
} else if (value instanceof String) {
args[j] = Date.valueOf(value.toString());
} else {
//返回错误,数据类型错误=========================================
throw new AppException(ajaxBean + "的参数和传入的参数类型不符。参数序号:" + ((Integer) j).toString() + ",参数类型:Date,传入类型:" + value.getClass());
}
}
}
} return args;
}
Object refObj = objectPool.getBean(refId);//自定义的对象池方法
if(refObj != null){
m.invoke(o, new Object[]{refObj});
}如果按着这种数组方式来调用的话
会报类型不匹配的异常java.lang.IllegalArgumentException: argument type mismatch
2楼所说的能得到方法需要传入的参数的类型.
知道类型后,传入相应类型的一个Object[]的参数就OK了.