JavaBean动态添加删除属性
程序员文章站
2022-04-03 10:27:22
...
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 clazz = obj.getClass(); Method[] methods = clazz.getDeclaredMethods();for (int i = 0; i从输出结果可以看出最后生成的obj只有id和username两个属性
2.org.apache.commons.beanutils
DynaProperty property = new DynaPropert
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);Mapmap = basicDynaBean.getMap(); Iterator it = map.keySet().iterator();while (it.hasNext()) { String key = it.next(); System.out.println(key + ":" + map.get(key)); } 输入结果: id:1username:name1 查看BasicDynaBean与BasicDynaClass之间的关系
DynaBean的源码
public interface DynaBean { public boolean contains(String name, String key); public Object get(String name); public Object get(String name, int index); public Object get(String name, String key); public DynaClass getDynaClass(); public void remove(String name, String key); public void set(String name, Object value); public void set(String name, int index, Object value); public void set(String name, String key, Object value); }主要是接口的定义
再来看看BasicDynaBean是怎么实现的,直接看public Object get(String name);
/** * 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 HashMapvalues = new HashMap (); 其实是用HashMap来实现的.
3.总结
用cglib动态删除添加属性时,虽然obj里有getUsername这个方法,却不能obj.getUsername()这样直接调用,想得到username的值只能通过beanMap.get("username")获取.
org.apache.commons.beanutils从源码来看是使用HashMap来实现的.
两种方式从操作角度来说和使用Map的区别不大.只是它们都提供了复制属性的工具方法.