1.cglib
BeanGenerator beanGenerator = new BeanGenerator();
beanGenerator.addProperty("id", Long.class);
beanGenerator.addProperty("username", String.class);
Object obj = beanGenerator.create();
BeanMap beanMap = BeanMap.create(obj);
BeanCopier copier = BeanCopier.create(User.class, obj.getClass(), false);
User user = new User();
user.setId(1L);
user.setUsername("name1");
user.setPassword("123");
copier.copy(user, obj, null);
System.out.println(beanMap.get("username"));
Class<? extends Object> clazz = obj.getClass();
System.out.println("---------");
Method[] methods = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println(methods[i].getName());
}
输出结果:
name1
---------
getId
setId
setUsername
getUsername
从输出结果可以看出最后生成的obj只有id和username两个属性
2.org.apache.commons.beanutils
DynaProperty property = new DynaProperty("id", Long.class);
DynaProperty property1 = new DynaProperty("username", String.class);
BasicDynaClass basicDynaClass = new BasicDynaClass("user", null, new DynaProperty[] { property, property1 });
BasicDynaBean basicDynaBean = new BasicDynaBean(basicDynaClass);
User user = new User();
user.setId(1L);
user.setUsername("name1");
user.setPassword("123");
BeanUtils.copyProperties(basicDynaBean, user);
Map<String, Object> map = basicDynaBean.getMap();
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key + ":" + map.get(key));
}
输入结果:
id:1
username:name1
查看BasicDynaBean与BasicDynaClass之间的关系 原 荐 JavaBean动态添加删除属性 DynaBean的源码
主要是接口的定义 再来看看BasicDynaBean是怎么实现的,直接看
/**
* Return the value of a simple property with the specified name.
*
* @param name Name of the property whose value is to be retrieved
* @return The property's value
*
* @exception IllegalArgumentException if there is no property
* of the specified name
*/
public Object get(String name) {
// Return any non-null value for the specified property
Object value = values.get(name);
if (value != null) {
return (value);
}
// Return a null value for a non-primitive property
Class<?> type = getDynaProperty(name).getType();
if (!type.isPrimitive()) {
return (value);
}
// Manufacture default values for primitive properties
if (type == Boolean.TYPE) {
return (Boolean.FALSE);
} else if (type == Byte.TYPE) {
return (new Byte((byte) 0));
} else if (type == Character.TYPE) {
return (new Character((char) 0));
} else if (type == Double.TYPE) {
return (new Double(0.0));
} else if (type == Float.TYPE) {
return (new Float((float) 0.0));
} else if (type == Integer.TYPE) {
return (new Integer(0));
} else if (type == Long.TYPE) {
return (new Long(0));
} else if (type == Short.TYPE) {
return (new Short((short) 0));
} else {
return (null);
}
}
从以上代码可以看出是在values里取值的
/**
* The set of property values for this DynaBean, keyed by property name.
*/
protected HashMap<String, Object> values = new HashMap<String, Object>();
其实是用HashMap来实现的.
3.总结
- 用cglib动态删除添加属性时,虽然obj里有getUsername这个方法,却不能obj.getUsername这样直接调用,想得到username的值只能通过beanMap.get("username")获取.
- org.apache.commons.beanutils从源码来看是使用HashMap来实现的.
- 两种方式从操作角度来说和使用Map的区别不大.只是它们都提供了复制属性的工具方法.