简体   繁体   English

通过反射获取bean属性getter或setter?

[英]Get bean property getter or setter by reflection?

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 . 假设我有一个类型对象的句柄,我通过配置告诉它它有一个int类型的bean属性,名称为age How can I retrieve the getter for this document? 如何检索此文档的getter?

Is there a better way than prepending "get" and capitalizing the "a" in age , and looking for a method of that name via reflection? 有没有一种更好的方法比在“ age ”中加上“获得”和大写“a”,并通过反思寻找一个名称的方法?

Take a look at java.beans.Introspector . 看看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();

See Also: 也可以看看:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Setter,getter方法的反映 - Reflection of setter, getter method 是否可以在Javadoc中引用bean的属性(getter和setter)? - Is there a way to refer bean's property (both getter and setter) in Javadoc? 使用getter和setter方法将列表添加到bean属性之间的区别? - Difference between using getter and setter methods to add list to a bean property? Bean 属性 'xxx' 不可读或具有无效的 getter 方法:getter 的返回类型是否与 setter 的参数类型匹配? - Bean property 'xxx' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? Bean属性“ cmpcode”不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配? - Bean property 'cmpcode' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? Bean属性'xxxx不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配 - Bean property 'xxxx is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter java 从属性名称获取 getter/setter 名称 - java get getter/setter name from property name Java反射 - 如何调用getter / setter方法? - Java reflection - how to call getter/setter method? 如何使用反射定义动态的setter和getter? - How to define dynamic setter and getter using reflection? Kotlin 中预期的属性 getter 或 setter - Property getter or setter expected in Kotlin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM