动态自定义javabean属性

13年前
在项目过程中遇到这样一个需求.某个bean的属性无法提前预知.需要在项目运行的过程中才能确定.刚开始的想法是用反射和代理.但是写了很长时间没写出来.后来在网上搜了搜.有人说用javasist和cglib都能实现.一下是解决方案,拿出来和大家分享.    package com.common;  import java.util.Iterator;    import java.util.Map;    import java.util.Set;        import net.sf.cglib.beans.BeanGenerator;  import net.sf.cglib.beans.BeanMap;  /**    *     * @author zyl    *    */    public class DynamicBean {            /**         * 实体Object         */        private  Object object = null;      /**         * 属�?map         */        private  BeanMap beanMap = null;        /**        * 无参构�?函数       */      public DynamicBean() {            super();        }        /**        * 构�?函数       */        @SuppressWarnings("unchecked")        public DynamicBean(Map propertyMap) {          this.object = generateBean(propertyMap);          this.beanMap = BeanMap.create(this.object);        }            /**         * 给bean属�?赋�?         * @param property 属�?�?        * @param value �?        */        public void setValue(String property, Object value) {          beanMap.put(property, value);        }            /**         * 通过属�?名得到属性�?         * @param property 属�?�?        * @return �?        */        public Object getValue(String property) {          return beanMap.get(property);        }            /**         * 得到该实体bean对象         * @return         */        public Object getObject() {          return this.object;        }            /**        * @param propertyMap        * @return        */        @SuppressWarnings("unchecked")        private Object generateBean(Map propertyMap) {          BeanGenerator generator = new BeanGenerator();          Set keySet = propertyMap.keySet();          for (Iterator i = keySet.iterator(); i.hasNext();) {         String key = (String) i.next();         generator.addProperty(key, (Class) propertyMap.get(key));          }          return generator.create();        }         }        测试类    package com.common;      import java.lang.reflect.Method;  import java.util.HashMap;      public class Test1 {  public static void main(String[] args) throws ClassNotFoundException {           HashMap propertyMap = new HashMap();              propertyMap.put("id", Class.forName("java.lang.Integer"));              propertyMap.put("name", Class.forName("java.lang.String"));              propertyMap.put("address", Class.forName("java.lang.String"));           propertyMap.put("age", Class.forName("java.lang.Integer"));           propertyMap.put("a", Class.forName("java.lang.Integer"));                               DynamicBean bean = new DynamicBean(propertyMap);                   bean.setValue("id", new Integer(123));             bean.setValue("name", "454");            bean.setValue("address", "789");                  bean.setValue("age", 123);            bean.setValue("a", 1);                        System.out.println("  >> id      = " + bean.getValue("id"));            System.out.println("  >> name    = " + bean.getValue("name"));            System.out.println("  >> address = " + bean.getValue("address"));            System.out.println("  >> age = " + bean.getValue("age"));                        Object object = bean.getObject();                Class clazz = object.getClass();            Method[] methods = clazz.getDeclaredMethods();            for (int i = 0; i < methods.length; i++) {                System.out.println(methods[i].getName());             }        }      }  大概就是这样了.