Spring配置文件中id的第二個(gè)字母不能大寫問題
今天遇到一個(gè)問題,在spring配置文件中的id第二個(gè)字母不能大寫,否則會(huì)產(chǎn)生異常:Bean property 'kManager' is not writable or has an invalid setter method. Did you mean 'KManager'?.為了解決問題研究了一下問題的原因:spring在autoWire的同時(shí)用到了jdk提供的java.beans.*目錄下的類(等有時(shí)間好好研究一下這些類,功能很強(qiáng)大),通過它們能夠得到bean的詳細(xì)信息。其中有個(gè)類PropertyDestriptor類能夠通過bean中的set/get方法找到property,不過有個(gè)小前提:property的命名要遵循第二個(gè)字母不能大寫。因?yàn)閖ava是國外開發(fā)的,它對(duì)命名遵循了英語的一個(gè)規(guī)范:大部分的單詞第二個(gè)字母都是小寫的,除了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) 評(píng)論(0) 編輯 收藏 所屬分類: 爪哇學(xué)習(xí)