數(shù)據(jù)加載中……
          Spring配置文件中id的第二個字母不能大寫問題

                 今天遇到一個問題,在spring配置文件中的id第二個字母不能大寫,否則會產(chǎn)生異常:Bean property 'kManager' is not writable or has an invalid setter method. Did you mean 'KManager'?.為了解決問題研究了一下問題的原因:spring在autoWire的同時用到了jdk提供的java.beans.*目錄下的類(等有時間好好研究一下這些類,功能很強(qiáng)大),通過它們能夠得到bean的詳細(xì)信息。其中有個類PropertyDestriptor類能夠通過bean中的set/get方法找到property,不過有個小前提:property的命名要遵循第二個字母不能大寫。因為java是國外開發(fā)的,它對命名遵循了英語的一個規(guī)范:大部分的單詞第二個字母都是小寫的,除了URL之類的單詞。
                  下面的java.beans.Introspector類中通過set/get方法找到property的代碼(希望大家可以自己去看看java.beans.*的代碼)。Open Source! I love it.

          /**
               * Utility method to take a string and convert it to normal Java variable
               * name capitalization.  This normally means converting the first
               * character from upper case to lower case, but in the (unusual) special
               * case when there is more than one character and both the first and
               * second characters are upper case, we leave it alone.
               * <p>
               * Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays
               * as "URL".
               *
               * @param  name The string to be decapitalized.
               * @return  The decapitalized version of the string.
               */
              public static String decapitalize(String name) {
           if (name == null || name.length() == 0) {
               return name;
           }
           if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
             Character.isUpperCase(name.charAt(0))){
               return name;
           }
           char chars[] = name.toCharArray();
           chars[0] = Character.toLowerCase(chars[0]);
           return new String(chars);
              }


          posted on 2007-12-05 14:27 牛浪de流 閱讀(794) 評論(0)  編輯  收藏 所屬分類: 爪哇學(xué)習(xí)

          主站蜘蛛池模板: 伊宁市| 彰化市| 白城市| 石嘴山市| 团风县| 台北市| 安达市| 北碚区| 二连浩特市| 盖州市| 乌兰察布市| 偃师市| 常宁市| 东明县| 潮州市| 天峨县| 长丰县| 商都县| 河曲县| 蒙自县| 班戈县| 五寨县| 密云县| 额济纳旗| 佛教| 宜宾市| 义乌市| 阿巴嘎旗| 临汾市| 行唐县| 安远县| 临高县| 邢台市| 新干县| 冕宁县| 鹤庆县| 裕民县| 东乌| 海兴县| 探索| 甘洛县|