java实体 动态添加属性_JavaBean动态添加删除属性

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

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 < methods.length; i++) {

System.out.println(methods[i].getName());

}

输出结果:

name1

getId

getUsername

setId

setUsername

从输出结果可以看出最后生成的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, newDynaProperty[]{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 map = 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之间的关系

c0ec13664ab170ceb260dc4d5b1baaee.png

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 HashMap values = new HashMap();

其实是用HashMap来实现的.

3.总结

用cglib动态删除添加属性时,虽然obj里有getUsername这个方法,却不能obj.getUsername()这样直接调用,想得到username的值只能通过beanMap.get("username")获取.

org.apache.commons.beanutils从源码来看是使用HashMap来实现的.

两种方式从操作角度来说和使用Map的区别不大.只是它们都提供了复制属性的工具方法.

需要的留邮哦~

7093b694376e20cce1a69e43eb1ef3f5.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值