Apache Commons BeanUtils

时间:2023-02-07 02:24:44

http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.2/apidocs/org/apache/commons/beanutils/package-summary.html#package_description

1. add maven

<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
<scope>compile</scope>
</dependency>

2. 参数赋值取值

3. 嵌套赋值取值

4.动态bean

DynaProperty[] props = new DynaProperty[]{

         new DynaProperty("address", java.util.Map.class),
new DynaProperty("subordinate", mypackage.Employee[].class),
new DynaProperty("firstName", String.class),
new DynaProperty("lastName", String.class)
};
BasicDynaClass dynaClass = new BasicDynaClass("employee", null, props);
DynaBean employee = dynaClass.newInstance();
employee.set("address", new HashMap());
employee.set("subordinate", new mypackage.Employee[0]);
employee.set("firstName", "Fred");
employee.set("lastName", "Flintstone");

MyBean bean = ...;
DynaBean wrapper = new WrapDynaBean(bean);
String firstName = wrapper.get("firstName"); LazyDynaBean 动态添加properties
5.类型转换
HttpServletRequest request = ...;
MyBean bean = ...;
HashMap map = new HashMap();
Enumeration names = request.getParameterNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
map.put(name, request.getParameterValues(name));
}
BeanUtils.populate(bean, map); BeanUtils.cloneBean(Object bean) BeanUtils.copyProperties(Object dest, Object orig)

BeanUtils.copyProperty(Object bean, String name, Object value)

BeanUtils.setProperty(Object bean, String name, Object value)
BeanUtils.getProperty(Object bean, String name)
6. 集合
1. 比较 BeanComparator