首頁 > Java > java教程 > JavaBean動態新增刪除屬性

JavaBean動態新增刪除屬性

高洛峰
發布: 2016-11-22 14:36:42
原創
3496 人瀏覽過

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, 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:1username:name1
登入後複製

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);


}
登入後複製
JavaBean動態新增刪除屬性主要是介面的定義

再來看看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&#39;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的區別不大.只是它們都提供了複製屬性的工具方法.

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板