the journey is the reward...

          常用鏈接

          統計

          最新評論

          DWR在和spring集成時的bug,SpringCreator.getType???

          DWR在和spring集成時,在dwr.xml中將設置creator="spring",告訴dwr將使用dwr的org.directwebremoting.spring.SpringCreator來創建對象實例,但是SpringCreator.getType地處理是不適當的,讓我們來看看它的源碼[dwr-3.0.0.116]:

          public Class<?> getType()
          {
          if (clazz == null)
          {
          try
          {
          clazz = getInstance().getClass();
          }
          catch (InstantiationException ex)
          {
          log.error("Failed to instansiate object to detect type.", ex);
          return Object.class;
          }
          }

          return clazz;
          }

          我們再來看看它的getInstance,最終由spring來創建實例.

          public Object getInstance() throws InstantiationException
          {
          try
          {
          if (overrideFactory != null)
          {
          return overrideFactory.getBean(beanName);
          }

          if (factory == null)
          {
          factory = getBeanFactory();
          }

          if (factory == null)
          {
          log.error("DWR can't find a spring config. See following info logs for solutions");
          log.info("- Option 1. In dwr.xml, <create creator='spring' ...> add
          log.info("- Option 2. Use a spring org.springframework.web.context.ContextLoaderListener.");
          log.info("- Option 3. Call SpringCreator.setOverrideBeanFactory() from your web-app");
          throw new InstantiationException("DWR can't find a spring config. See the logs for solutions");
          }

          return factory.getBean(beanName);
          }
          catch (InstantiationException ex)
          {
          throw ex;
          }
          catch (Exception ex)
          {
          throw new InstantiationException("Illegal Access to default constructor on " + clazz.getName() + " due to: " + ex);
          }
          }

          getInstance將返回由spring來創建的實例,很明顯SpringCreator.getType有點多此一舉,它先創建了實例,再從實例的getClass獲取對象的類型,而spring的beanFactory.getType同樣有此功能,但它不需要先創建實例.

          也許寫這位代碼的仁兄是不知道spring beanFactory.getType這個方法吧!


          我把SpringCreator.getType改正后的代碼 如下:

          public Class<?> getType()
          {
          if (clazz == null)
          {
          try
          {
          if(overrideFactory != null){
          clazz=overrideFactory.getType(beanName);
          }else {
          if(factory == null)
          factory = getBeanFactory();
          clazz=factory.getType(beanName);
          }

          }
          catch (Exception ex)
          {
          log.error("Failed to detect type.", ex);
          return Object.class;
          }
          }

          return clazz;
          }

          如果出現 Error loading class for creator ...... 那么就修改SpringCreator吧!

          posted on 2010-02-10 12:04 adapterofcoms 閱讀(804) 評論(0)  編輯  收藏 所屬分類: java techs

          主站蜘蛛池模板: 赣州市| 贞丰县| 松桃| 汶川县| 灵川县| 石渠县| 辽宁省| 兴宁市| 保德县| 成武县| 大荔县| 沈阳市| 洪洞县| 丰城市| 徐汇区| 利津县| 柘荣县| 富宁县| 屏边| 礼泉县| 巴中市| 长汀县| 溧阳市| 新竹市| 紫云| 平和县| 娱乐| 区。| 漳平市| 华蓥市| 常山县| 岫岩| 泰州市| 民权县| 贡山| 鄯善县| 正蓝旗| 昌乐县| 无棣县| 百色市| 万盛区|