1
有没有一种方法可以指定特定bean中属性的字符串名称并返回该getter对应的类?Java反射属性字符串名称 - >类数据类型
有没有一种方法可以指定特定bean中属性的字符串名称并返回该getter对应的类?Java反射属性字符串名称 - >类数据类型
您可以使用java.beans.Introspector类获取有关给定bean的信息。您无法查询BeanInfo为特定属性,但可以遍历他们:
private Class<?> getPropertyType(Class<?> clazz, String property) {
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] propDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propDescriptor : propDescriptors) {
// String name of a property
if (property.equals(propDescriptor.getName())) {
// Class the getter corresponds to.
return propDescriptor.getPropertyType();
}
}
...
}
发现... org.apache.commons.beanutils.PropertyUtils.getPropertyType(对象豆,字符串名称)
你知道BeanUtil是否可以做到这一点? – 2010-02-16 11:31:39
我想是这样,但在提出问题之前,您可能应该声明您使用的是Apache Commons! – 2010-02-16 11:36:25