俊星的BLOG

          我的DWR之DefaultContainer

          DWR中采用DefaultContainer來(lái)加載默認(rèn)的配置信息,下面是我的實(shí)現(xiàn):
          import java.beans.BeanInfo;
          import java.beans.Introspector;
          import java.beans.PropertyDescriptor;
          import java.lang.reflect.Method;
          import java.util.Collection;
          import java.util.HashMap;
          import java.util.Map;

          public class CantainerTest {
              
          public static void main(String[] args) {
                  Cantainer cant 
          = new Cantainer();
                  
          try {
                      cant.addParam(
          "name""can_name");
                      cant.addParam(
          "bool""true");
                      cant.addParam(
          "age""111");
                      cant.addParam(
          "beanOne"new BeanOne());
                      cant.addParam(BeanTwo.
          class.getName(), "BeanTwo");
                      cant.finishConfig();

                      System.out.println(cant.getBean(
          "name"));
                      System.out.println(cant.getBean(
          "bool"));
                      System.out.println(cant.getBean(
          "age"));
                      System.out.println(cant.getBean(
          "beanOne"));
                      System.out.println(cant.getBean(BeanTwo.
          class.getName()));

                  }
           catch (Exception e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }

              }

          }


          class Cantainer {
              
          private Map<String, Object> beans = new HashMap<String, Object>();

              
          public void addParam(String key, Object value) throws Exception {
                  
          if (value instanceof String) {
                      
          try {
                          Class impl 
          = Class.forName((String) value);
                          value 
          = impl.newInstance();
                      }
           catch (ClassNotFoundException e) {
                      }

                  }

                  beans.put(key, value);
              }


              
          public void finishConfig() throws Exception {
                  Collection
          <Object> col = beans.values();
                  
          for (Object obj : col) {
                      
          if (!(obj instanceof String)) {
                          
          // 開始自動(dòng)匹配
                          BeanInfo bi = Introspector.getBeanInfo(obj.getClass());
                          
          for (PropertyDescriptor desc : bi.getPropertyDescriptors()) {
                              Object value 
          = beans.get(desc.getName());
                              
          if (value != null{
                                  Method setter 
          = desc.getWriteMethod();
                                  Class propType 
          = setter.getParameterTypes()[0];
                                  
          boolean invokeFlg = false;
                                  
          if (value.getClass().isAssignableFrom(propType)) {
                                      invokeFlg 
          = true;
                                  }
           else if (propType == Boolean.TYPE && value.getClass() == String.class{
                                      value 
          = Boolean.valueOf((String) value);
                                      invokeFlg 
          = true;
                                  }
           else if (propType == Integer.TYPE && value.getClass() == String.class{
                                      value 
          = Integer.valueOf((String) value);
                                      invokeFlg 
          = true;
                                  }
           else if (propType == Boolean.TYPE && value.getClass() == String.class{
                                      value 
          = Boolean.valueOf((String) value);
                                      invokeFlg 
          = true;
                                  }

                                  
          if (invokeFlg) {
                                      setter.invoke(obj, 
          new Object[] { value });
                                  }

                              }

                          }

                      }

                  }

              }


              
          public Object getBean(String key) {
                  
          return beans.get(key);
              }

          }


          class BeanOne {
              
          public BeanOne() {
                  
          super();
                  
          this.name = "name";
              }


              
          private String name;

              
          public String getName() {
                  
          return name;
              }


              
          public void setName(String name) {
                  
          this.name = name;
              }


              
          public String toString() {
                  
          return "BeanOne name:" + this.name;
              }

          }


          class BeanTwo {
              
          private BeanOne beanOne;
              
          private boolean bool;
              
          private int age;

              
          public BeanTwo() {
                  
          this.beanOne = new BeanOne();
                  
          this.bool = false;
                  
          this.age = 123;
              }


              
          public BeanOne getBeanOne() {
                  
          return beanOne;
              }


              
          public void setBeanOne(BeanOne beanOne) {
                  
          this.beanOne = beanOne;
              }


              
          public boolean isBool() {
                  
          return bool;
              }


              
          public void setBool(boolean bool) {
                  
          this.bool = bool;
              }


              
          public int getAge() {
                  
          return age;
              }


              
          public void setAge(int age) {
                  
          this.age = age;
              }


              
          public String toString() {
                  
          return "BeanTwo beanOne:<" + this.beanOne + "> bool:" + this.bool + " age:" + this.age;
              }

          }

          輸出如下:
          can_name
          true
          111
          BeanOne name:can_name
          BeanTwo beanOne:<BeanOne name:can_name> bool:true age:111

          posted on 2009-05-20 22:45 俊星 閱讀(238) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 太湖县| 调兵山市| 福安市| 开江县| 教育| 广河县| 沐川县| 泽普县| 广汉市| 贺州市| 长子县| 墨竹工卡县| 珲春市| 新密市| 鲁山县| 松阳县| 镇江市| 华安县| 上犹县| 南靖县| 图们市| 韩城市| 镇雄县| 东山县| 华池县| 定南县| 云南省| 宝丰县| 临沧市| 宜丰县| 富阳市| 宣武区| 龙口市| 泾源县| 大田县| 临桂县| 隆子县| 铁岭县| 偃师市| 尤溪县| 丹阳市|