「日常记录」Java实用工具 - org.apache.commons.beanutils.BeanUtils

2021/8/11 20:08:17

本文主要是介绍「日常记录」Java实用工具 - org.apache.commons.beanutils.BeanUtils,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

什么是 BeanUtils?

官方解释

Commons BeanUtils
Most Java developers are used to creating Java classes that conform to the JavaBeans naming patterns for property getters and setters. It is natural to then access these methods directly, using calls to the corresponding getXxx and setXxx methods. However, there are some occasions where dynamic access to Java object properties (without compiled-in knowledge of the property getter and setter methods to be called) is needed. Example use cases include:

Building scripting languages that interact with the Java object model (such as the Bean Scripting Framework).
Building template language processors for web presentation and similar uses (such as JSP or Velocity).
Building custom tag libraries for JSP and XSP environments (such as Jakarta Taglibs, Struts, Cocoon).
Consuming XML-based configuration resources (such as Ant build scripts, web application deployment descriptors, Tomcat’s server.xml file).
The Java language provides Reflection and Introspection APIs (see the java.lang.reflect and java.beans packages in the JDK Javadocs). However, these APIs can be quite complex to understand and utilize. The BeanUtils component provides easy-to-use wrappers around these capabilities.

个人理解

Apache的Commons包中的BeanUtils工具类,提供了对于JavaBean进行各种操作, 比如对象,属性复制等。

  • 克隆JavaBean
    cloneBean(final Object bean)
    传入需要克隆的JavaBean,会返回一个克隆后的JavaBean,且原JavaBean不变。
  • JavaBean2Map
    describe(final Object bean)
    传入需要转换的JavaBean,会返回一个Map<String, String>的集合,原JavaBean不变;
    P.S.:该方法会将JavaBean的所有属性进行转换,包括父类的属性。


这篇关于「日常记录」Java实用工具 - org.apache.commons.beanutils.BeanUtils的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程