俊星的BLOG

          我的DWR之DefaultContainer

          DWR中采用DefaultContainer來加載默認的配置信息,下面是我的實現:
          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)) {
                          
          // 開始自動匹配
                          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 俊星 閱讀(237) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 景泰县| 乐清市| 佛学| 盐津县| 余姚市| 高淳县| 南昌县| 绍兴县| 屯留县| 通渭县| 防城港市| 射洪县| 鄂州市| 辛集市| 黄平县| 平武县| 河东区| 沐川县| 综艺| 凭祥市| 南涧| 株洲市| 文安县| 寿宁县| 岳阳县| 祥云县| 榆社县| 溆浦县| 泽库县| 紫阳县| 元江| 基隆市| 阳信县| 民权县| 泸州市| 沂南县| 靖江市| 米脂县| 莱阳市| 肇源县| 焉耆|