Java BeanUtils.getProperty()的替代项

Java BeanUtils.getProperty()的替代项,java,apache-commons,Java,Apache Commons,我正在寻找BeanUtils.getProperty()的替代项。我想要使用替代项的唯一原因是避免最终用户再拥有一个依赖项 我正在处理一个自定义约束,这是我的一段代码 final Object firstObj = BeanUtils.getProperty(value, this.firstFieldName); final Object secondObj = BeanUtils.getProperty(value, this.secondFieldName); 因为我需要从对象中得到这两

我正在寻找BeanUtils.getProperty()的替代项。我想要使用替代项的唯一原因是避免最终用户再拥有一个依赖项

我正在处理一个自定义约束,这是我的一段代码

final Object firstObj = BeanUtils.getProperty(value, this.firstFieldName);
final Object secondObj = BeanUtils.getProperty(value, this.secondFieldName);
因为我需要从对象中得到这两个属性。
没有第三方系统的情况下,是否有其他替代方案,或者我需要从
BeanUtilsBean
复制这段代码?

BeanUtils非常强大,因为它支持嵌套属性。例如“bean.prop1.prop2”,将
Map
s处理为bean和dynabean

例如:

 HashMap<String, Object> hashMap = new HashMap<String, Object>();
 JTextArea value = new JTextArea();
 value.setText("jArea text");
 hashMap.put("jarea", value);

 String property = BeanUtils.getProperty(hashMap, "jarea.text");
 System.out.println(property);

如果您使用SpringFramework,“BeanRapperImpl”就是您要寻找的答案:

BeanWrapperImpl wrapper = new BeanWrapperImpl(sourceObject);

Object attributeValue = wrapper.getPropertyValue("attribute");
注意:从2020年起,Spring建议不要直接实例化
BeanWrapperImpl
,而是使用
PropertyAccessorFactory.forBeanPropertyAccess(sourceObject)
BeanWrapperImpl wrapper = new BeanWrapperImpl(sourceObject);

Object attributeValue = wrapper.getPropertyValue("attribute");