通过反射获取bean属性getter或setter? [英] Get bean property getter or setter by reflection?
本文介绍了通过反射获取bean属性getter或setter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个类型对象的句柄,我被配置告知它有一个类型为 int
的bean属性,名称为年龄
。如何检索此文档的getter?
Suppose I have a handle on an object of type , and I'm told by configuration that it has a bean property of type int
with the name age
. How can I retrieve the getter for this document?
有没有比在年龄
中预先设置获取和大写a更好的方法,并寻找通过反射得到该名称的方法?
Is there a better way than prepending "get" and capitalizing the "a" in age
, and looking for a method of that name via reflection?
推荐答案
看看 java.beans.Introspector
。此类允许您获取类的属性列表。
Take a look at java.beans.Introspector
. This class allows you to get the list of properties on a class.
如果您知道属性名称,则可以调用
If you know property name you can call
Method getter = new PropertyDescriptor(propertyName, beanClass).getReadMethod();
参见:
java.beans.PropertyDescriptor
java.lang.reflect.Method
这篇关于通过反射获取bean属性getter或setter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文